我想在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 ..
这是一个不使用apply的解决方案:
输出:
相关问题 更多 >
编程相关推荐