分组数据处理后向数据框添加新列时出错

2024-06-08 06:10:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在groupby('score')之后添加特定列patient的第25百分位信息,但得到的错误如下所示

import pandas as pd

raw_data = {'patient': [242, 151, 111,122, 342],
        'obs': [1, 2, 3, 1, 2],
        'treatment': [0, 1, 0, 1, 0],
        'score': ['strong', 'weak', 'weak', 'weak', 'strong']}

df = pd.DataFrame(raw_data, columns = ['patient', 'obs', 'treatment', 'score'])

df

   patient  obs  treatment   score
0      242    1          0  strong
1      151    2          1    weak
2      111    3          0    weak
3      122    1          1    weak
4      342    2          0  strong


quantile_25 = []
df_g=df.groupby("score")

for col in df.keys():
    if col=='patient':

        Q1 = df_g.apply(lambda _df: _df.np.percentile(_df[feature], q = 25))
        quantile_25.append(Q1)

    else:
        pass

df['std_dev_patient'] = df.score.map(quantile_25[0])

AttributeError: Cannot access callable attribute 'groupby' of >'DataFrameGroupBy' objects, try using the 'apply' method

我想保持相同的for loop,因为我想添加其他统计信息作为新列

泰铢

预期产量

   patient  obs  treatment   score   quantile_25
0      242    1          0  strong     ..
1      151    2          1    weak     ..
2      111    3          0    weak     ..
3      122    1          1    weak     ..
4      342    2          0  strong     ..

Tags: 信息dffordatarawcolstrongpd
1条回答
网友
1楼 · 发布于 2024-06-08 06:10:17

这是一个不使用apply的解决方案:

df_g=df.groupby("score")
for col in df.columns:
    if col=='patient':
        df['std_dev_patient'] = df_g[col].transform(lambda group: np.percentile(group, q=25))
    else:
        pass

输出:

   patient  obs  treatment   score  std_dev_patient
0      242    1          0  strong            267.0
1      151    2          1    weak            116.5
2      111    3          0    weak            116.5
3      122    1          1    weak            116.5
4      342    2          0  strong            267.0

相关问题 更多 >

    热门问题