我正在嘗試創(chuàng)建一個包含組均值的新列,條件是另一列的值。這可以通過示例來最好地解釋:df = pd.DataFrame({'A': [59000000, 65000000, 434000, 434000, 434000, 337000, 11300, 11300, 11300], 'B': [1, 1 , 0, 1, 0, 0, 1, 1, 0], 'group': ["IT", "IT", "IT", "MV", "MV", "MV", "IT", "MV", "MV"]})df A B group0 59000000 1 IT1 65000000 1 IT2 434000 0 IT3 434000 1 MV4 434000 0 MV5 337000 0 MV6 11300 1 IT7 11300 1 MV8 11300 0 MV我已經設法解決了這個問題,但我正在尋找代碼行更少,可能更有效率的東西。x = df.loc[df['B']==1].groupby('group', as_index=False)['A'].mean()x.rename(columns = {'A':'a'}, inplace = True)df = pd.merge(df, x, how='left', on='group') A B group a0 59000000 1 IT 413371001 65000000 1 IT 413371002 434000 0 IT 413371003 434000 1 MV 2226504 434000 0 MV 2226505 337000 0 MV 2226506 11300 1 IT 413371007 11300 1 MV 2226508 11300 0 MV 222650我嘗試過使用轉換函數(shù),但它對我不起作用df.loc[: , 'a'] = df.groupby('group').transform(lambda x: x[x['B']==1]['A'].mean())
1 回答

qq_花開花謝_0
TA貢獻1835條經驗 獲得超7個贊
使用 Series.where
僅篩選所需的 col 值,然后分組
并轉換
:A
df['a'] = df['A'].where(df['B'].eq(1)).groupby(df['group']).transform('mean')
[輸出]
A B group a
0 59000000 1 IT 41337100.0
1 65000000 1 IT 41337100.0
2 434000 0 IT 41337100.0
3 434000 1 MV 222650.0
4 434000 0 MV 222650.0
5 337000 0 MV 222650.0
6 11300 1 IT 41337100.0
7 11300 1 MV 222650.0
8 11300 0 MV 222650.0
添加回答
舉報
0/150
提交
取消