2014-12-03 97 views
29

所有列最佳方式我有一个数据帧:大熊猫:选择开始X

import pandas as pd 
import numpy as np 

df = pd.DataFrame({'foo.aa': [1, 2.1, np.nan, 4.7, 5.6, 6.8], 
        'foo.fighters': [0, 1, np.nan, 0, 0, 0], 
        'foo.bars': [0, 0, 0, 0, 0, 1], 
        'bar.baz': [5, 5, 6, 5, 5.6, 6.8], 
        'foo.fox': [2, 4, 1, 0, 0, 5], 
        'nas.foo': ['NA', 0, 1, 0, 0, 0], 
        'foo.manchu': ['NA', 0, 0, 0, 0, 0],}) 

我想在列选择开始foo.值1。有没有更好的办法做到这一点以外:类似于编写类似

df2 = df[(df['foo.aa'] == 1)| 
(df['foo.fighters'] == 1)| 
(df['foo.bars'] == 1)| 
(df['foo.fox'] == 1)| 
(df['foo.manchu'] == 1) 
] 

东西:

df2= df[df.STARTS_WITH_FOO == 1] 

答案应该打印出这样的数据帧:

bar.baz foo.aa foo.bars foo.fighters foo.fox foo.manchu nas.foo 
0  5.0  1.0   0    0  2   NA  NA 
1  5.0  2.1   0    1  4   0  0 
2  6.0  NaN   0   NaN  1   0  1 
5  6.8  6.8   1    0  5   0  0 

[4 rows x 7 columns] 

回答

45

刚执行列表理解来创建您的列:

In [28]: 

filter_col = [col for col in df if col.startswith('foo')] 
filter_col 
Out[28]: 
['foo.aa', 'foo.bars', 'foo.fighters', 'foo.fox', 'foo.manchu'] 
In [29]: 

df[filter_col] 
Out[29]: 
    foo.aa foo.bars foo.fighters foo.fox foo.manchu 
0  1.0   0    0  2   NA 
1  2.1   0    1  4   0 
2  NaN   0   NaN  1   0 
3  4.7   0    0  0   0 
4  5.6   0    0  0   0 
5  6.8   1    0  5   0 

另一种方法是创建一个从列系列和使用矢量化STR方法startswith

In [33]: 

df[df.columns[pd.Series(df.columns).str.startswith('foo')]] 
Out[33]: 
    foo.aa foo.bars foo.fighters foo.fox foo.manchu 
0  1.0   0    0  2   NA 
1  2.1   0    1  4   0 
2  NaN   0   NaN  1   0 
3  4.7   0    0  0   0 
4  5.6   0    0  0   0 
5  6.8   1    0  5   0 

为了达到你想要什么,你需要添加以下过滤不符合你的价值观==1标准:

In [36]: 

df[df[df.columns[pd.Series(df.columns).str.startswith('foo')]]==1] 
Out[36]: 
    bar.baz foo.aa foo.bars foo.fighters foo.fox foo.manchu nas.foo 
0  NaN  1  NaN   NaN  NaN  NaN  NaN 
1  NaN  NaN  NaN    1  NaN  NaN  NaN 
2  NaN  NaN  NaN   NaN  1  NaN  NaN 
3  NaN  NaN  NaN   NaN  NaN  NaN  NaN 
4  NaN  NaN  NaN   NaN  NaN  NaN  NaN 
5  NaN  NaN   1   NaN  NaN  NaN  NaN 

编辑

OK看到你想要的东西后,令人费解的答案是这样的:

In [72]: 

df.loc[df[df[df.columns[pd.Series(df.columns).str.startswith('foo')]] == 1].dropna(how='all', axis=0).index] 
Out[72]: 
    bar.baz foo.aa foo.bars foo.fighters foo.fox foo.manchu nas.foo 
0  5.0  1.0   0    0  2   NA  NA 
1  5.0  2.1   0    1  4   0  0 
2  6.0  NaN   0   NaN  1   0  1 
5  6.8  6.8   1    0  5   0  0 
+0

请考虑您的选项2移动到顶部您的回答 – JanLauGe 2018-02-26 15:31:46

+0

downvoter谨慎解释? – EdChum 2018-02-26 16:35:59

+0

@JanLauGe虽然'startswith'将是纯粹的'pandas'方法,但使用列表理解实际上是最快的方法,所以我已经发布了两种方法 – EdChum 2018-02-26 16:36:38

17

现在,大熊猫的索引支持字符串操作,可以说是最简单和最好的方式选择开头列‘富’仅仅是:

df.loc[:, df.columns.str.startswith('foo')] 

或者,您也可以过滤柱(或行)标签df.filter()。要指定一个正则表达式匹配的名称与foo.开始:

>>> df.filter(regex=r'^foo\.', axis=1) 
    foo.aa foo.bars foo.fighters foo.fox foo.manchu 
0  1.0   0    0  2   NA 
1  2.1   0    1  4   0 
2  NaN   0   NaN  1   0 
3  4.7   0    0  0   0 
4  5.6   0    0  0   0 
5  6.8   1    0  5   0 

只选择所需的行(含1)和列,您可以使用loc,用filter选择列(或任何其他方法)和行使用any

>>> df.loc[(df == 1).any(axis=1), df.filter(regex=r'^foo\.', axis=1).columns] 
    foo.aa foo.bars foo.fighters foo.fox foo.manchu 
0  1.0   0    0  2   NA 
1  2.1   0    1  4   0 
2  NaN   0   NaN  1   0 
5  6.8   1    0  5   0 
+0

那么选择部分呢?其中df [cols] == 1被应用,我会做一个for循环或者有更快的方法吗? – ccsv 2014-12-03 15:37:20

+0

@ccsv我想你可以直接写'df.filter(regex = r'^ foo \。',axis = 1)== 1'(让我知道我是否误解了你想要的)。 – 2014-12-03 15:38:44

+0

它是关闭你只需要转换布尔值并删除一些行。如果你运行的代码,我有它的所有列,但只有行0,1,2,5,因为它们的值为1的标题为'foo' – ccsv 2014-12-03 15:49:12

1

我的解决方案。这可能是对性能较慢:

a = pd.concat(df[df[c] == 1] for c in df.columns if c.startswith('foo')) 
a.sort_index() 


    bar.baz foo.aa foo.bars foo.fighters foo.fox foo.manchu nas.foo 
0  5.0  1.0   0    0  2   NA  NA 
1  5.0  2.1   0    1  4   0  0 
2  6.0  NaN   0   NaN  1   0  1 
5  6.8  6.8   1    0  5   0  0 
1

所希望的项目的选择另一种选择是使用map

df.loc[(df == 1).any(axis=1), df.columns.map(lambda x: x.startswith('foo'))] 

,让你所有的列包含一个1行:

foo.aa foo.bars foo.fighters foo.fox foo.manchu 
0  1.0   0    0  2   NA 
1  2.1   0    1  4   0 
2  NaN   0   NaN  1   0 
5  6.8   1    0  5   0 

行选

完成
(df == 1).any(axis=1) 

在@ ajcr的答案,让你:

0  True 
1  True 
2  True 
3 False 
4 False 
5  True 
dtype: bool 

意味着该行34不包含1,不会被选中。

选择列的使用布尔索引这样做是:

df.columns.map(lambda x: x.startswith('foo')) 

在上面这个例子返回

array([False, True, True, True, True, True, False], dtype=bool) 

所以,如果一列不以foo开始,返回False,因此未选择该列。

如果你只想返回包含一个1所有行 - 如您所需的输出暗示 - 你可以简单地做

df.loc[(df == 1).any(axis=1)] 

返回

bar.baz foo.aa foo.bars foo.fighters foo.fox foo.manchu nas.foo 
0  5.0  1.0   0    0  2   NA  NA 
1  5.0  2.1   0    1  4   0  0 
2  6.0  NaN   0   NaN  1   0  1 
5  6.8  6.8   1    0  5   0  0