簡體   English   中英

Python:獲取 pandas dataframe 每一行的最大值列

[英]Python: Get Columns of max values each row of an pandas dataframe

python 中獲取 pandas 數據幀矩陣(沒有索引列)中每行最大值的列號的最佳方法是什么。 例如,如果我有

日期 公司 1 公司 2 公司 3
01.01.2020 23 21 14
02.01.2020 22 12 22
03.01.2020 11 11 12
…… ... ... ...
02.01.2020 2 14 3

output 應該是向量:

 [1, 1, 3, ..., 2]

使用idxmax

In [949]: cols = df.iloc[:, 1:].idxmax(1).tolist()

In [950]: cols
Out[950]: ['Company 1', 'Company 1', 'Company 3']

如果你想要column index

In [951]: [df.columns.get_loc(i) for i in cols]
Out[951]: [1, 1, 3]

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM