2 回答

TA貢獻1803條經驗 獲得超3個贊
讓我們按組提取列然后映射:
max_cols = (df.filter(like='value') # choose the value columns, also df.iloc[:, :4]
.groupby(df['group']).sum() # calculate sum per group
.idxmax(axis=1) # find col with max value
)
df['Column'] = df['group'].map(max_cols)
還groupby().transform():
df['Column'] = (df.filter(like='value')
.groupby(df['group']).transform('sum')
.idxmax(axis=1)
)
輸出:
value1 value2 value3 value4 random string column group Column
index1 10 2 3 4 stuff group 2 value1
index2 5 4 3 2 other stuff group 1 value4
index3 6 7 8 9 other stuff group 1 value4
index4 1 2 2 4 yet other stuff group 2 value1
index5 6 1 8 11 other stuff group 1 value4

TA貢獻1851條經驗 獲得超5個贊
這是map和的一種潛在解決方案dictionary:
#creates a dictionary with the maximum sum per group
d = df.groupby('group').sum().idxmax(axis=1).to_dict()
#mapping the dictionary to 'group' column to generated a new column
df['Group Identifying Column'] = df['group'].map(d)
或者,您可以切斷該dictionary部分并簡單地執行以下操作:
df['Group Identifying Column'] = df.group.map(df.groupby('group').sum().idxmax(axis=1))
輸出:
value1 value2 ... group Group Identifying Column
index1 10 2 ... group 2 value1
index2 5 4 ... group 1 value4
index3 6 7 ... group 1 value4
index4 1 2 ... group 2 value1
index5 6 1 ... group 1 value4
添加回答
舉報