简体   繁体   English

Pandas 如何在 select 列中一行具有最大值?

[英]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.

相关问题 Select Pandas dataframe 行,其中两列或多列一起具有最大值 - Select Pandas dataframe row where two or more columns have their maximum value together 如何 select pandas 行在一个列中具有最大值,来自一组共享两个公共列的行? - How to select pandas row with maximum value in one column, from a group of rows that share two common columns? Select a pandas dataframe 行,其中列具有最小值 - Select a pandas dataframe row where column has minimum value 熊猫-选择最大value_counts行 - pandas - select row of maximum value_counts 根据两列的函数在 Pandas 数据框中选择列值最大的行 - Select row in Pandas dataframe where a column value is max based on a function of two columns Python pandas:选择特定行满足条件的列 - Python pandas: Select columns where a specific row satisfies a condition 如何获取熊猫数据框中行值不为零的列数计数 - How to get count of number of columns where the value is not zero row-wise in a pandas dataframe Pandas - 如果特定列的值为1,则将行中的其他列替换为0 - Pandas - Replace other columns in row with 0 if a specific column has a value of 1 如何在任何行在 Python pandas dataframe 中具有 NaN 值后删除列 - How to remove columns after any row has a NaN value in Python pandas dataframe 如何在 pandas df 中的 select 行中的值也出现在过滤列中? - How to select rows in pandas df where value appears also in filtered columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM