gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 07:21:00 24 4
gpt4 key购买 nike

我想在 groupby('score') 之后添加特定列 patent 的 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 循环,因为我想添加其他统计信息作为新列。

谢谢

预期输出

   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 ..

最佳答案

这是一个不使用 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

关于python - 分组数据处理后向数据框中添加新列时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57704451/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com