根据多个条件删除数据框行

时间:2017-04-27 16:46:52

标签: python pandas dataframe

我试图根据需要在同一行中遇到的4个条件从pandas DataFrame中删除一些行。 所以我尝试了以下命令:

my_data.drop(my_data[(my_data.column1 is None) & (my_data.column2 is None) & (my_data.column3 is None) & (my_data.column4 is None)].index, inplace=True)

它会抛出此错误: enter image description here

我也试过了:

my_data= my_data.loc[my_data[(my_data.column1 is None) & (my_data.column2 is None) & (my_data.column3 is None) & (my_data.column4 is None), :]

但没有成功

我可以得到一些帮助:)

我正在使用python 3.5

1 个答案:

答案 0 :(得分:4)

通常,使用isnull方法检查列的空值:

df.drop(df[df['column1'].isnull() 
          & df['column2'].isnull() 
          & df['column3'].isnull() 
          & df['column4'].isnull()].index)

然而,有更紧凑和惯用的方式:

df.dropna(subset=['column1', 'column2', 'column3', 'column4'], how='all')

演示:

prng = np.random.RandomState(0)
df = pd.DataFrame(prng.randn(100, 6), columns=['column{}'.format(i) for i in range(1, 7)])

df.head()
Out: 
    column1   column2   column3   column4   column5   column6
0  1.764052  0.400157  0.978738  2.240893  1.867558 -0.977278
1  0.950088 -0.151357 -0.103219  0.410599  0.144044  1.454274
2  0.761038  0.121675  0.443863  0.333674  1.494079 -0.205158
3  0.313068 -0.854096 -2.552990  0.653619  0.864436 -0.742165
4  2.269755 -1.454366  0.045759 -0.187184  1.532779  1.469359

df = df.mask(prng.binomial(1, 0.5, df.shape).astype('bool'), np.nan)

df.head()
Out: 
    column1   column2   column3   column4   column5   column6
0       NaN  0.400157       NaN  2.240893       NaN       NaN
1  0.950088 -0.151357 -0.103219  0.410599  0.144044       NaN
2  0.761038  0.121675       NaN       NaN       NaN -0.205158
3       NaN       NaN -2.552990       NaN  0.864436       NaN
4  2.269755 -1.454366  0.045759 -0.187184       NaN       NaN

仅当列1,3,5和6为空时,以下内容才会删除行:

df.dropna(subset=['column1', 'column3', 'column5', 'column6'], how='all').head()
Out: 
    column1   column2   column3   column4   column5   column6
1  0.950088 -0.151357 -0.103219  0.410599  0.144044       NaN
2  0.761038  0.121675       NaN       NaN       NaN -0.205158
3       NaN       NaN -2.552990       NaN  0.864436       NaN
4  2.269755 -1.454366  0.045759 -0.187184       NaN       NaN
5  0.154947  0.378163 -0.887786 -1.980796 -0.347912       NaN