[英]Pandas how to select columns where a row has maximum value?
Let's say I have following dataframe:假设我关注 dataframe:
df = pd.DataFrame({'A':[0.1,0.1,0.2],
'B': [0.22,0.2,0.22],
'C': [0.3, 0.2,0.333]})
df.index = list('abc')
df
A B C
a 0.1 0.22 0.300
b 0.1 0.20 0.200
c 0.2 0.22 0.333
How to select all the columns where index b has maximum value.如何 select 索引 b 具有最大值的所有列。
Here, index has maximum value 2 in columns B, and C.这里,索引在 B 列和 C 中具有最大值 2。 So, the required answer is:
所以,需要的答案是:
required_answer = ['B','C']
My attempts:我的尝试:
df[df.loc['b'] ==df.loc['b'].max()]
You can try this:你可以试试这个:
df.columns[df.loc['b'] == df.loc['b'].max()].tolist()
# ['B', 'C']
I give you several options.我给你几个选择。 The first is the best:
第一个是最好的:
[*df.columns[df.loc['b'].eq(df.loc['b'].max())]]
['B', 'C']
df.loc[:,df.loc['b'].eq(df.loc['b'].max())].columns.tolist()
['B', 'C']
[*df.T.index[df.loc['b'].eq(df.loc['b'].max())]]
['B', 'C']
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.