Pandas使用bool过滤DataFrame的列

时间:2016-05-23 12:52:05

标签: python pandas dataframe

对于具有多个列和行的DataFrame(df)

     A   B  C  D
0    1   4  2  6
1    2   5  7  4
2    3   6  5  6

和另一个包含dtype的数据框(dfBool):bool

0  True
1  False
2  False
3  True

通过转置dfbool将这个DataFrame按列拆分为两个不同的DataFrame,以便获得所需的输出,这是最简单的方法

     A   D
0    1   6
1    2   4
2    3   6 

     B  C 
0    4  2  
1    5  7  
2    6  5  

我无法理解,在我有限的经历中,为什么dfTrue = df[dfBool.transpose() == True]不起作用

2 个答案:

答案 0 :(得分:5)

我想修改EdChum's comment,因为如果dfBoolDataFrame,则必须先选择column

import pandas as pd

df = pd.DataFrame({'D': {0: 6, 1: 4, 2: 6},
                    'A': {0: 1, 1: 2, 2: 3},
                    'C': {0: 2, 1: 7, 2: 5},
                    'B': {0: 4, 1: 5, 2: 6}})
print (df)
   A  B  C  D
0  1  4  2  6
1  2  5  7  4
2  3  6  5  6

dfBool = pd.DataFrame({'a':[True, False, False, True]})
print (dfBool)
       a
0   True
1  False
2  False
3   True
#select first column in dfBool
df2 = (dfBool.iloc[:,0])
#or select column a in dfBool
#df2 = (dfBool.a)
print (df2)
0     True
1    False
2    False
3     True
Name: a, dtype: bool

print (df[df.columns[df2]])
   A  D
0  1  6
1  2  4
2  3  6

print (df[df.columns[~df2]])
   B  C
0  4  2
1  5  7
2  6  5

来自ayhan的另一个非常好的解决方案,谢谢:

print (df.loc[:, dfBool.a.values])
   A  D
0  1  6
1  2  4
2  3  6

print (df.loc[:, ~dfBool.a.values])
   B  C
0  4  2
1  5  7
2  6  5

但如果dfBoolSeries,则解决方案效果非常好:

dfBool = pd.Series([True, False, False, True])
print (dfBool)

0     True
1    False
2    False
3     True
dtype: bool

print (df[df.columns[dfBool]])
   A  D
0  1  6
1  2  4
2  3  6

print (df[df.columns[~dfBool]])
   B  C
0  4  2
1  5  7
2  6  5

对于Series

print (df.loc[:, dfBool.values])
   A  D
0  1  6
1  2  4
2  3  6

print (df.loc[:, ~dfBool.values])
   B  C
0  4  2
1  5  7
2  6  5

<强>计时

In [277]: %timeit (df[df.columns[dfBool.a]])
1000 loops, best of 3: 769 µs per loop

In [278]: %timeit (df.loc[:, dfBool1.a.values])
The slowest run took 9.08 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 380 µs per loop

In [279]: %timeit (df.transpose()[dfBool1.a.values].transpose())
The slowest run took 5.04 times longer than the fastest. This could mean that an intermediate result is being cached.
1000 loops, best of 3: 550 µs per loop

时间安排的代码

import pandas as pd

df = pd.DataFrame({'D': {0: 6, 1: 4, 2: 6},
                    'A': {0: 1, 1: 2, 2: 3},
                    'C': {0: 2, 1: 7, 2: 5},
                    'B': {0: 4, 1: 5, 2: 6}})
print (df)
df = pd.concat([df]*1000, axis=1).reset_index(drop=True)

dfBool = pd.DataFrame({'a': [True, False, False, True]})
dfBool1 = pd.concat([dfBool]*1000).reset_index(drop=True)

输出略有不同:

print (df[df.columns[dfBool.a]])
   A  A  A  A  A  A  A  A  A  A ...  D  D  D  D  D  D  D  D  D  D
0  1  1  1  1  1  1  1  1  1  1 ...  6  6  6  6  6  6  6  6  6  6
1  2  2  2  2  2  2  2  2  2  2 ...  4  4  4  4  4  4  4  4  4  4
2  3  3  3  3  3  3  3  3  3  3 ...  6  6  6  6  6  6  6  6  6  6

[3 rows x 2000 columns]

print (df.loc[:, dfBool1.a.values])
   A  D  A  D  A  D  A  D  A  D ...  A  D  A  D  A  D  A  D  A  D
0  1  6  1  6  1  6  1  6  1  6 ...  1  6  1  6  1  6  1  6  1  6
1  2  4  2  4  2  4  2  4  2  4 ...  2  4  2  4  2  4  2  4  2  4
2  3  6  3  6  3  6  3  6  3  6 ...  3  6  3  6  3  6  3  6  3  6

[3 rows x 2000 columns]

print (df.transpose()[dfBool1.a.values].transpose())
   A  D  A  D  A  D  A  D  A  D ...  A  D  A  D  A  D  A  D  A  D
0  1  6  1  6  1  6  1  6  1  6 ...  1  6  1  6  1  6  1  6  1  6
1  2  4  2  4  2  4  2  4  2  4 ...  2  4  2  4  2  4  2  4  2  4
2  3  6  3  6  3  6  3  6  3  6 ...  3  6  3  6  3  6  3  6  3  6

[3 rows x 2000 columns]

答案 1 :(得分:2)

可能是以下情况?

import pandas as pd
totalDF = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [2, 7, 5], 'D': [6, 4, 8]})

dfBool = pd.DataFrame(data=[True, False, False, True])

totalDF.transpose()[dfBool.values].transpose()


   A  D
0  1  6
1  2  4
2  3  8

totalDF.transpose()[~dfBool.values].transpose()

   B  C
0  4  2
1  5  7
2  6  5