I want to check if one entire column of a dataframe is empty ! So let's say that I have
data1 = [12,34,'',678,896]
data2 = ['','','','','']
df = pd.DataFrame(list(zip(data1,data2)),columns = ['Data1','Data2'])
print(df)
Data1 Data2
0 12
1 34
2
3 678
4 896
The column "Data1" has data and the column "Data2" is empty. I tried the function print(df['Data2'].empty)
, but it returns me false and I don't get why. So how can I verify that ?
Thank you
If there are blank spaces,
it might not be all ''
so it is better to use regex
here, then we can use Series.isna()
and Series.all()
df['Data2'].replace(r'^\s*$', np.nan, regex=True).isna().all()
You can check if the Series
is empty by first converting ''(Blank)
to np.nan
and then dropna()
:
In [2530]: import numpy as np
In [2531]: df.Data2 = df.Data2.replace('', np.nan)
In [2533]: df.Data2.dropna().empty
Out[2533]: True
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With