沿列而不是行的数据框布尔选择 [英] dataframe boolean selection along columns instead of row
本文介绍了沿列而不是行的数据框布尔选择的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我具有以下数据框:
Suppose I have the following dataframe:
a b c d
0 0.049531 0.408824 0.975756 0.658347
1 0.981644 0.520834 0.258911 0.639664
2 0.641042 0.534873 0.806442 0.066625
3 0.764057 0.063252 0.256748 0.045850
,我只希望第0行中的值小于0.5的列的子集.我可以这样做:
and I want only the subset of columns whose value in row 0 is creater than 0.5. I can do this:
df2 = df.T
myResult = df2[df2.iloc[:, 0] > 0.5].T
但这感觉就像是骇人听闻的骇客.有没有更好的方法来沿列进行布尔索引?我可以在某个地方指定轴参数吗?
But this feels like a horrible hack. Is there a nicer way to do boolean indexing along columns? Somewhere I can specify an axis argument?
推荐答案
如何?
df.loc[:, df.iloc[0, :] > 0.5]
这篇关于沿列而不是行的数据框布尔选择的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文