Search code examples
pythonpandasdataframeis-empty

Python : How can I check if the content of one entire column of a Dataframe is empty?


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


Solution

  • 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()