Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

check if a dataframe is not empty in 1 line of code in python

I am checking if a dataframe is empty or not but i want to do this in 1 line of code as i am checking many dataframe’s and don’t want to repeat the same code again as it becomes chunky. example is below. great if anyone can help

if not df.empty:
   df['test_col'] = np.nan
else:
   print('data is empty')

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

you can do a for loop:

list_of_dfs=[df1,df2,df3] # there can be any number of dfs
for x in list_of_dfs:
  if not x.empty:
   x['test_col'] = np.nan
  else:
   print('data is empty')
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading