首页 > 解决方案 > 从 idxmax 获取系列

问题描述

假设我有一个这样的数据框:

    A   B   C
0  123  3   2
1   2   8   1
2   4   11  3

3 rows x 3 columns

我有它各自的idxmax()系列

A  0
B  2
3  2
Lenght: 3, dtype: int64

现在,我有第二个数据框:

    A   B   C
0  'a'  7  'c'
1  'b'  5  'a'
2  'c'  2  'd'

因此,使用idxmax()之前计算的系列,我想检索每列中的相应值,并获取系列

A 'a'
B  5
C 'd'
Lenght: 3, dtype: object

标签: pandas

解决方案


DataFrame.lookup与构造函数一起使用Series

s = df1.idxmax()

a = pd.Series(df2.lookup(s, s.index), index=s.index)
print (a)
A    'a'
B    '2'
C    'd'
dtype: object

推荐阅读