【问题标题】:Create groups based on column values根据列值创建组
【发布时间】:2019-05-16 11:27:49
【问题描述】:

我正在尝试根据特定的 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 的一个 sn-p,它有超过 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

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

【问题讨论】:

  • 如果total_usage 是 64,user_group 会是什么?
  • @rsno 我想这将取决于人口中的其他价值如何。我的数据集有 6000 多条记录,因此我尝试以编程方式将这些记录分组。
  • @jezrael 在这个例子中是的。但是,我的完整 DataFrame 包含 6000 多条记录,具有数百个唯一值。
  • @moe_95 - 所以如果 200 个唯一值需要 1st_group200th_group
  • @jezrael 不,我希望将整个人口分成 10 组。我的完整 df 有 6000 条记录,其中大部分是唯一的(由于小数位)。

标签: python pandas


【解决方案1】:

看起来您正在寻找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

【讨论】:

  • 非常感谢您的帮助。在我的整个 DataFrame 上运行时,我收到以下错误 ValueError: Bin edges must be unique: array([-451., -19., -13., -10., -8., -7., -6., -5., -4.,-4., -1.]). You can drop duplicate edges by setting the 'duplicates' kwarg
  • @moe_95 见this question
  • 非常感谢,我现在去看看
【解决方案2】:

qcut 用于改变负数的顺序,将Series.map 用于1.st2.nd 值:

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

【讨论】:

  • 非常感谢您的帮助,非常感谢。在我的整个 DataFrame 上运行时,我收到以下错误 ValueError: Bin edges must be unique: array([-451., -19., -13., -10., -8., -7., -6., -5., -4.,-4., -1.]). You can drop duplicate edges by setting the 'duplicates' kwarg
【解决方案3】:

尝试将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

【讨论】:

    猜你喜欢
    • 2019-12-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-23
    • 1970-01-01
    • 2020-10-24
    • 2021-12-28
    • 2022-01-08
    相关资源
    最近更新 更多