[英]find 3 largest values in every column in data frame and get the index number python
I have data frame like this 我有这样的数据框
A B C D
0 0.037949 0.021150 0.127416 0.040137
1 0.025174 0.007935 0.011774 0.003491
2 0.022339 0.019022 0.024849 0.018062
3 0.017205 0.051902 0.033246 0.018605
4 0.044075 0.044006 0.065896 0.021264
And I want to get the data frame with the index values of 3 largest values in each columns. 我想获得每列中索引值为3个最大值的数据框。 Desired output
所需的输出
A B C D
0 4 3 0 0
1 0 4 4 4
2 1 0 3 3
Given 特定
>>> df
A B C D
0 0.037949 0.021150 0.127416 0.040137
1 0.025174 0.007935 0.011774 0.003491
2 0.022339 0.019022 0.024849 0.018062
3 0.017205 0.051902 0.033246 0.018605
4 0.044075 0.044006 0.065896 0.021264
you can use DataFrame.apply
in combination with Series.nlargest
: 您可以将
DataFrame.apply
与Series.nlargest
结合使用:
>>> df.apply(lambda s: pd.Series(s.nlargest(3).index))
A B C D
0 4 3 0 0
1 0 4 4 4
2 1 0 3 3
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.