Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 查找数据帧中最大值的索引_Python_Pandas - Fatal编程技术网

Python 查找数据帧中最大值的索引

Python 查找数据帧中最大值的索引,python,pandas,Python,Pandas,我有一个数据框,有m行和n列。 我想找到每行中出现的最大值的列索引 我尝试使用idxmax,但它只返回第一次出现的max值。数据帧每行有多个最大值,我想获得每行中最大值的所有索引。下面是一个使用numpy的示例: import pandas as pd import numpy as np df = pd.DataFrame(np.random.randint(0, 5, (3, 10))) print(df) 0 1 2 3 4 5 6 7 8 9 0 3 0

我有一个数据框,有m行和n列。 我想找到每行中出现的最大值的列索引


我尝试使用idxmax,但它只返回第一次出现的max值。数据帧每行有多个最大值,我想获得每行中最大值的所有索引。

下面是一个使用
numpy
的示例:

import pandas as pd
import numpy as np

df = pd.DataFrame(np.random.randint(0, 5, (3, 10)))

print(df)

   0  1  2  3  4  5  6  7  8  9
0  3  0  0  2  2  4  0  2  1  2
1  3  4  0  4  4  1  1  0  2  3
2  4  1  3  4  4  4  0  3  1  0

res = [np.where(i == np.max(i)) for i in df.values]

print(res)

[array([5], dtype=int64),
 array([1, 3, 4], dtype=int64),
 array([0, 3, 4, 5], dtype=int64)]