如何使用idxmax输出子集熊猫数据框列?

时间:2019-10-20 05:17:57

标签: python pandas dataframe

我有一个熊猫数据框:

import numpy as np
import pandas as pd
df = pd.DataFrame(np.random.randint(0,40,size=(10,4)), columns=range(4), index = range(10))
df.head()

    0   1   2   3
0  27  10  13  21
1  25  12  23   8
2   2  24  24  34
3  10  11  11  10
4   0  15   0  27

我正在使用idxmax函数来获取包含最大值的列。

df_max = df.idxmax(1)
df_max.head()

0    0
1    0
2    3
3    1
4    3

如何与df_max一起使用df,以创建与df每行中的最大值相对应的值的时间序列?这是我想要的输出:

0    27
1    25
2    34
3    11
4    27
5    37
6    35
7    32
8    20
9    38

我知道我可以使用df.max(1)来实现这一点,但是我想知道如何使用df_max来达到相同的输出,因为我希望能够将df_max应用于与df共享相同列和索引(但值不相同)的其他矩阵(不是df)。

1 个答案:

答案 0 :(得分:3)

您可以尝试df.lookup

df.lookup(df_max.index, df_max)

Out[628]: array([27, 25, 34, 11, 27], dtype=int64)

如果要使用Series / DataFrame,请将输出传递给Series / DataFrame构造函数

pd.Series(df.lookup(df_max.index, df_max), index=df_max.index)

Out[630]:
0    27
1    25
2    34
3    11
4    27
dtype: int64