简体   繁体   English

过滤熊猫DataFrame列

[英]Filter pandas DataFrame columns

I have a DataFrame like this: 我有一个像这样的DataFrame:

             A        B        C        D      
2000-01-03 -0.59885  0.18141 -0.68828  0.77572
2000-01-04  0.83935  0.15993  0.95911 -1.12959
2000-01-05  2.80215 -0.10858 -1.62114 -0.20170
2000-01-06  0.71670 -0.26707  1.36029  1.74254

I would like to filter the columns based on the value of the first row. 我想根据第一行的值过滤列。 Eg I want to take only the columns where the first value is >0. 例如,我只想取第一个值> 0的列。 and the result I expect is this: 我期望的结果是:

             B        D      
2000-01-03  0.18141  0.77572
2000-01-04  0.15993 -1.12959
2000-01-05 -0.10858 -0.20170
2000-01-06 -0.26707  1.74254

Update Thanks to Jeff suggestion I wrote this code: 更新由于Jeff的建议,我编写了以下代码:

cols = []
firstRow = df.ix[0,:]
for i in range(len(firstRow)):
    if firstRow[i]>0:
    cols.append(i) 
return df.ix[:, list(cols)].values.copy()   

Is there a more elegant way to do this? 有没有更优雅的方法可以做到这一点?

This is obviously using the data generated below, but you can easily apply to your example. 显然,这是使用下面生成的数据,但是您可以轻松地将其应用于示例。 The iloc[-2] selects the 2nd to last row, and creates a boolean array The loc then takes that boolean array and select the applicable columns iloc[-2]选择倒数第二行,并创建一个布尔数组。然后loc取该布尔数组并选择适用的列

In [2]: df = DataFrame(np.random.randn(4,4),columns=list('ABCD'),
             index=date_range('20000103',periods=4))

In [3]: df
Out[3]: 
                   A         B         C         D
2000-01-03 -0.132896 -0.151352  0.960943 -0.007701
2000-01-04 -1.653279 -1.101331 -2.083493 -1.920517
2000-01-05 -1.190868  0.983487  0.804209  0.962575
2000-01-06  0.232290  2.152097  0.414457  1.023253

In [6]: df.loc[:,df.iloc[-2]<0]
Out[6]: 
                   A
2000-01-03 -0.132896
2000-01-04 -1.653279
2000-01-05 -1.190868
2000-01-06  0.232290

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM