python - Pandas:找到最大值的列名,我需要过滤带有星号文本的列?

标签 python pandas

我有这样的数据,

enter image description here

所以我将每个评级(星星)的百分比存储到 pandas 中,如下所示

enter image description here

现在我想获取最大值的列名,例如

1_star  2_star  3_star  4_star  5_star
0.023   0.112   0.474   0.316   0.075

## returns 3_star

我该怎么做?

我发现一个答案非常适合我的情况,Pandas second largest value's column name ,但似乎太复杂了。毕竟我可以通过df.max()获取最大值,为什么获取最大值的列名这么复杂?

最佳答案

看来你需要 Series.idxmaxiloc选择系列行:

print (df)
   1_star  2_star  3_star  4_star  5_star
0   0.023   0.112   0.474   0.316   0.075

print (df.iloc[0].idxmax())
3_star

如果需要所有DataFrame的值,请使用DataFrame.idxmax - 输出是系列:

print (df.idxmax(axis=1))

您还可以按 filter过滤包含_star文本的列:

print (df.filter(like='_star').idxmax(axis=1))

关于python - Pandas:找到最大值的列名,我需要过滤带有星号文本的列?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42291442/

相关文章:

python - 将字符串转换为列表

python - 重新分配字典值列表

Pandas 0.21.1 - DataFrame.replace 递归错误

python - 根据索引条件从 Pandas DataFrame 中删除行

python - 如何使用 Python Pandas 将 CSV 文件写入 XLSX?

python - 将时间列标题与 DataFrame Pandas 中行中的相应日期连接起来

python - 有没有一种快速的方法来找到两个大字节序列不同的第一个偏移量?

python - GStreamer:将延迟/间隙/移位引入音频

python - 通过 REPL 工作,但从命令行运行时失败 : AttributeError: module 'talib' has no attribute 'MA'

python - 多索引数据框连接(如果不是 NaN 则保留更新的数据并附加到新索引)