Python 3.x 最大值的列标签(以大熊猫为单位)
我试图从数据框中提取行和列标签中的最大值。比如说,Python 3.x 最大值的列标签(以大熊猫为单位),python-3.x,pandas,Python 3.x,Pandas,我试图从数据框中提取行和列标签中的最大值。比如说, A B C D index x 0 1 2 3 y 3 2 1 0 我期望得到以下结果 A B C D Maxv Con index x 0 1 2 3 3 D y 3 2 1 0 3 A 我试了以下方法 df['Maxv'] = df.apply(max,axis=1) df['Con'] = df.idxmax(axis='rows') 它
A B C D
index
x 0 1 2 3
y 3 2 1 0
我期望得到以下结果
A B C D Maxv Con
index
x 0 1 2 3 3 D
y 3 2 1 0 3 A
我试了以下方法
df['Maxv'] = df.apply(max,axis=1)
df['Con'] = df.idxmax(axis='rows')
它只返回max列,Con列返回“NaN”。这里的错误是什么
提前谢谢
AP需要
axis='columns'
或axis=1
in:
或:
您将得到NaN
s,因为数据未与索引对齐
:
print (df.idxmax(axis='rows'))
A y
B y
C x
D x
dtype: object
df['Con'] = df.idxmax(axis=1)
print (df)
A B C D Maxv Con
index
x 0 1 2 3 3 D
y 3 2 1 0 3 A
print (df.idxmax(axis='rows'))
A y
B y
C x
D x
dtype: object