3

I want to check if a Pandas Dataframe row contains an empty set in a specific column, i.e.

d = {'col1': [1, 2], 'col2': [3, {}]}
df2 = pd.DataFrame(data=d)


    col1    col2
0   1       3
1   2       {}

and then

df2['col_2_contains_empty_set'] = ? #  how to implement this

should give

    col1    col2    col_2_contains_empty_set
0   1       3       False
1   2       {}      True

What's the correct way to do this? Can't do

bool(df['col2']) 

or

df['col2'].bool()

as Series are have ambiguous Boolean values, I think.

Zubo
  • 1,543
  • 2
  • 20
  • 26

4 Answers4

6

One way:

df2.apply(lambda x: any(x.values == {}), axis=1)

Output:

0    False
1     True
dtype: bool

OR

df2['c'] = np.max(df2.values == {}, 1).astype(bool)

Output:

   col1 col2      c
0     1    3  False
1     2   {}   True
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
3

You can take advantage of the fact that len({})=0 and apply a lambda function:

df2['col2'].apply(lambda x: len(x)==0)

Note that this will return True for empty lists and dicts as well.

2

You can just compare df2.values to an empty dictionary:

In [ ]: df2['col_2_contains_empty_set'] = (df2.values == {}).any(axis=1)
   ...: df2
Out[ ]: 
   col1 col2  col_2_contains_empty_set
0     1    3                     False
1     2   {}                      True
Sebastian Mendez
  • 2,859
  • 14
  • 25
0
df2.applymap(type)==type({})
Out[1044]: 
    col1   col2
0  False  False
1  False   True

after assgin it back

df2['C']=(df2.applymap(type)==type({})).any(1)
df2
Out[1052]: 
   col1 col2      C
0     1    3   False
1     2   {}    True
BENY
  • 317,841
  • 20
  • 164
  • 234