如何使用列名作为变量基于 null/not null 过滤数据帧?

Los*_*oul 16 python pandas

我想列出一个数据框,其中特定列为空或不为空,我让它工作使用 -

df[df.Survive.notnull()] # contains no missing values 
df[df.Survive.isnull()]  #---> contains missing values 
Run Code Online (Sandbox Code Playgroud)

这工作得很好,但我想让我的代码更加动态,并将“Survive”列作为变量传递,但它对我不起作用。

我试过:

variableToPredict = ['Survive'] 
df[df[variableToPredict].notnull()]
Run Code Online (Sandbox Code Playgroud)

我收到错误 -ValueError: cannot reindex from a duplicate axis

我确信我犯了一个愚蠢的错误,我能做些什么来解决这个问题?

jez*_*ael 25

所以想法总是需要系列或列表或一维数组作为过滤掩码。

如果只想测试一列,请使用标量:

variableToPredict = 'Survive'
df[df[variableToPredict].notnull()]
Run Code Online (Sandbox Code Playgroud)

但是,如果 add[]输出是一列DataFrame,则有必要更改测试函数any(测试每行是否至少有一个 NaN,在多列中有意义)或all(测试每行是否有所有 NaN,在多列中有意义)函数:

variableToPredict = ['Survive']
df[df[variableToPredict].notnull().any(axis=1)]

variableToPredict = ['Survive', 'another column']
df[df[variableToPredict].notnull().any(axis=1)]
Run Code Online (Sandbox Code Playgroud)

样本

df = pd.DataFrame({'Survive':[np.nan, 'A', 'B', 'B', np.nan],
                   'another column':[np.nan, np.nan, 'a','b','b']})

print (df)
  Survive another column
0     NaN            NaN
1       A            NaN
2       B              a
3       B              b
4     NaN              b
Run Code Online (Sandbox Code Playgroud)

首先如果只测试一列:

variableToPredict = 'Survive'
df1 = df[df[variableToPredict].notnull()]
print (df1)
  Survive another column
1       A            NaN
2       B              a
3       B              b

print (type(df[variableToPredict]))
<class 'pandas.core.series.Series'>

#Series
print (df[variableToPredict])
0    NaN
1      A
2      B
3      B
4    NaN
Name: Survive, dtype: object

print (df[variableToPredict].isnull())
0     True
1    False
2    False
3    False
4     True
Name: Survive, dtype: bool
Run Code Online (Sandbox Code Playgroud)

如果使用列表 - 这里是一个元素列表:

variableToPredict = ['Survive']
print (type(df[variableToPredict]))
<class 'pandas.core.frame.DataFrame'>

#one element DataFrame
print (df[variableToPredict])
  Survive
0     NaN
1       A
2       B
3       B
4     NaN
Run Code Online (Sandbox Code Playgroud)

any如果每行测试,则or的输出相同all

print (df[variableToPredict].notnull().any(axis=1))
0    False
1     True
2     True
3     True
4    False
dtype: bool

print (df[variableToPredict].notnull().all(axis=1))
0    False
1     True
2     True
3     True
4    False
dtype: bool
Run Code Online (Sandbox Code Playgroud)

如果测试列表中的一列或多列:

variableToPredict = ['Survive', 'another column']

print (type(df[variableToPredict]))
<class 'pandas.core.frame.DataFrame'>

print (df[variableToPredict])
  Survive another column
0     NaN            NaN
1       A            NaN
2       B              a
3       B              b
4     NaN              b
Run Code Online (Sandbox Code Playgroud)
print (df[variableToPredict].notnull())
   Survive  another column
0    False           False
1     True           False
2     True            True
3     True            True
4    False            True


#at least one NaN per row, at least one True
print (df[variableToPredict].notnull().any(axis=1))
0    False
1     True
2     True
3     True
4     True
dtype: bool

#all NaNs per row, all Trues 
print (df[variableToPredict].notnull().all(axis=1))
0    False
1    False
2     True
3     True
4    False
dtype: bool
Run Code Online (Sandbox Code Playgroud)