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

Python: Can I use if else function when creating a new dataset?

I’m a beginner in python.

I need to find out what function should I use in order to create a new dataset (df2) for empty original dataset (df). For example,

df2 = df['count_days'].dt.days >= 1

When I don’t have no days to count in the dataset (empty data), I get an error.

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

Can I use if else statement to resolve this issue?
For example:

if df['count_days'].dt.days >= 1
   df2
else 
   print("No Data")

BTW – I sometimes would have empty dataset because there is no record available.

Thank you!

>Solution :

Yes you can use the if and else statements:

import pandas as pd

if df.empty:
    print("No Data")
else:
    df2 = df['count_days'].dt.days >= 1
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