3 回答

TA贡献1847条经验 获得超11个赞
您可以使用idxmaxwith axis=1查找每一行上具有最大值的列:
>>> df.idxmax(axis=1)
0 Communications
1 Business
2 Communications
3 Communications
4 Business
dtype: object
要创建新的列“ Max”,请使用df['Max'] = df.idxmax(axis=1)。
要查找每列中出现最大值的行索引,请使用df.idxmax()(或等效地df.idxmax(axis=0))。

TA贡献1824条经验 获得超6个赞
您可以apply在数据框上并argmax()通过获取每一行axis=1
In [144]: df.apply(lambda x: x.argmax(), axis=1)
Out[144]:
0 Communications
1 Business
2 Communications
3 Communications
4 Business
dtype: object
这里有一个基准来比较慢apply的方法来idxmax()进行len(df) ~ 20K
In [146]: %timeit df.apply(lambda x: x.argmax(), axis=1)
1 loops, best of 3: 479 ms per loop
In [147]: %timeit df.idxmax(axis=1)
10 loops, best of 3: 47.3 ms per loop

TA贡献1820条经验 获得超2个赞
如果要生成包含最大值的列名但仅考虑列子集的列,则可以使用@ajcr答案的变体:
df['Max'] = df[['Communications','Business']].idxmax(axis=1)
添加回答
举报