根据列值创建组

时间:2019-05-16 11:27:50

标签: python pandas

我正在尝试根据特定的DataFrame列值创建用户组。我想基于total_usage指标创建整个DataFrame总体的10个用户组。下面显示了一个示例DataFrame df

user_id   total_usage
1         10
2         10
3         20
4         20
5         30
6         30
7         40
8         40
9         50
10        50
11        60
12        60
13        70
14        70
15        80
16        80
17        90
18        90
19        100
20        100

df只是整个DataFrame的一个片段,该片段的长度超过6000个记录,但是我只希望有10个用户组。

下面是我期望的输出示例。

user_id   total_usage  user_group
1         10           10th_group
2         10           10th_group
3         20           9th_group
4         20           9th_group
5         30           8th_group
6         30           8th_group
7         40           7th_group
8         40           7th_group
9         50           6th_group
10        50           6th_group
11        60           5th_group
12        60           5th_group
13        70           4th_group
14        70           4th_group
15        80           3th_group
16        80           3th_group
17        90           2nd_group
18        90           2nd_group
19        100          1st_group
20        100          1st_group

任何人都可以提供的任何帮助将不胜感激。

3 个答案:

答案 0 :(得分:2)

1.st2.nd值使用qcut并按负数更改顺序,对{{1}和s = pd.qcut(-df['total_usage'], np.arange(0,1.1, 0.1), labels=False) + 1 d = {1:'st', 2:'nd'} df['user_group'] = s.astype(str) + s.map(d).fillna('th') + '_group' print (df) user_id total_usage user_group 0 1 10 10th_group 1 2 10 10th_group 2 3 20 9th_group 3 4 20 9th_group 4 5 30 8th_group 5 6 30 8th_group 6 7 40 7th_group 7 8 40 7th_group 8 9 50 6th_group 9 10 50 6th_group 10 11 60 5th_group 11 12 60 5th_group 12 13 70 4th_group 13 14 70 4th_group 14 15 80 3th_group 15 16 80 3th_group 16 17 90 2nd_group 17 18 90 2nd_group 18 19 100 1st_group 19 20 100 1st_group 使用Series.map

$format

答案 1 :(得分:2)

看起来您正在寻找qcut,但顺序相反

df['user_group'] = 10 - pd.qcut(df['total_usage'], np.arange(0,1.1, 0.1)).cat.codes

输出,它不是序数,但我希望它能做到:

0     10
1     10
2      9
3      9
4      8
5      8
6      7
7      7
8      6
9      6
10     5
11     5
12     4
13     4
14     3
15     3
16     2
17     2
18     1
19     1
dtype: int8

答案 2 :(得分:0)

尝试将pd.Seriesnp.repeatnp.arangepd.DataFrame.groupbypd.Series.astypepd.Series.mappd.Series.fillna一起使用:

x = df.groupby('total_usage')
s = pd.Series(np.repeat(np.arange(len(x.ngroups), [len(i) for i in x.groups.values()]) + 1)
df['user_group'] = (s.astype(str) + s.map({1: 'st', 2: 'nd'}).fillna('th') + '_Group').values[::-1]

现在:

print(df)

是:

    user_id  total_usage  user_group
0         1           10  10th_Group
1         2           10  10th_Group
2         3           20   9th_Group
3         4           20   9th_Group
4         5           30   8th_Group
5         6           30   8th_Group
6         7           40   7th_Group
7         8           40   7th_Group
8         9           50   6th_Group
9        10           50   6th_Group
10       11           60   5th_Group
11       12           60   5th_Group
12       13           70   4th_Group
13       14           70   4th_Group
14       15           80   3th_Group
15       16           80   3th_Group
16       17           90   2nd_Group
17       18           90   2nd_Group
18       19          100   1st_Group
19       20          100   1st_Group