【问题标题】:Applying pandas cut to grouped items where bin depends on column value将 pandas cut 应用于 bin 取决于列值的分组项目
【发布时间】:2020-01-16 07:35:03
【问题描述】:

this answer 开始,我想将具有不同 bin 大小的剪切应用于分组表。比如我想转这个

+------+--------+--------+
| User | Value1 | Value2 |
+------+--------+--------+
|    1 |     56 |     12 |
|    1 |     28 |     30 |
|    1 |     65 |     42 |
|    2 |    394 |     33 |
|    2 |      2 |     43 |
|    2 |     60 |     45 |
|    3 |      5 |     20 |
|    3 |      5 |     80 |
+------+--------+--------+

进入这个

+------+--------+--------+-----------+
| User | Value1 | Value2 |  Interval |
+------+--------+--------+-----------+
|    1 |     56 |     12 |           |
|    1 |     28 |     30 | (12, 30]  |
|    1 |     65 |     42 | (30, 42]  |
|    2 |    394 |     33 |           |
|    2 |      2 |     43 | (33, 43]  |
|    2 |     60 |     45 | (43, 45]  |
|    3 |      5 |     20 |           |
|    3 |      5 |     80 | (20, 80]  |
+------+--------+--------+-----------+

按照this answer 的问题“在 groupby 中应用 pandas cut”,到目前为止我的尝试是

import pandas

data = {
    'User': [1, 1, 1, 2, 2, 2, 3, 3],
    'Value1': [56, 28, 65, 394, 2, 60, 5, 5],
    'Value2': [12, 30, 42, 33, 43, 45, 20, 80]}

df = pd.DataFrame.from_dict(data)

df['Interval'] = df.groupby(['User']).transform(lambda x: pd.cut(x['Value2'], bins=x['Value2']))

但这给出了错误

KeyError: ('Value2', '发生在索引 Value1')

【问题讨论】:

    标签: pandas


    【解决方案1】:

    只需将applycut 一起使用

    df['Interval'] = df.groupby('User').Value2.apply(lambda x: pd.cut(x, bins=x))
    
    Out[1997]:
       User  Value1  Value2      Interval
    0     1      56      12           NaN
    1     1      28      30  (12.0, 30.0]
    2     1      65      42  (30.0, 42.0]
    3     2     394      33           NaN
    4     2       2      43  (33.0, 43.0]
    5     2      60      45  (43.0, 45.0]
    6     3       5      20           NaN
    7     3       5      80  (20.0, 80.0]
    

    如果你更喜欢transform,它仍然工作如下

    df['Interval'] = df.groupby('User').Value2.transform(lambda x: pd.cut(x, bins=x))
    
    Out[2000]:
       User  Value1  Value2      Interval
    0     1      56      12           NaN
    1     1      28      30  (12.0, 30.0]
    2     1      65      42  (30.0, 42.0]
    3     2     394      33           NaN
    4     2       2      43  (33.0, 43.0]
    5     2      60      45  (43.0, 45.0]
    6     3       5      20           NaN
    7     3       5      80  (20.0, 80.0]
    

    【讨论】:

    • 谢谢。我对为什么 df.groupby(['User'])['Value2'].apply(lambda x: pd.cut(x, bins=x)) 有效但 df.groupby(['User']).apply(lambda x: pd.cut(x['Value2'], bins=x['Value2'])) 失败感到困惑,他们看起来应该对我做同样的事情
    • @dumbledad:一个在SeriesGroupby 上调用apply,另一个在DataFrameGroupby 上调用。两者都返回相同的输出箱。但是SeriesGroupby的结果是单级索引,而DataFrameGroupby是多级索引。因此,由于索引不同/不兼容,DataFrameGroupby 在将结果分配回df 时失败。
    猜你喜欢
    • 2020-03-11
    • 2010-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多