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

Remove Empty DataFrame in pandas

I’ve got this following dataframe that contains an empty row because the value missing for selected slot (which is a time):

    slot       tempo
10  7--8  132.559556
   slot     tempo
8  7--8  135.0565
   slot       tempo
9  7--8  125.582778
   slot       tempo
7  7--8  117.038667
   slot       tempo
9  7--8  135.946333
Empty DataFrame
Columns: [slot, tempo]
Index: []
   slot       tempo
2  7--8  123.476571
   slot       tempo
3  7--8  125.724286
   slot    tempo
2  7--8  139.503
   slot       tempo
2  7--8  140.977429
   slot       tempo
1  7--8  135.035875
   slot       tempo
1  7--8  120.741556

The code i used to obtain this df is:

path = os.getcwd()
csv_files = glob.glob(os.path.join(path, "*.csv"))

for f in csv_files:
    dfDay = pd.read_csv(f, encoding = "ISO-8859-1", sep = ';')
    
    dfSlotMean = dfDay.groupby('slot', as_index=False)['tempo'].mean()
    
    slotMorningMean = dfSlotMean[dfSlotMean.slot == '7--8']
    # print(type(slotMorningMean))
    print((slotMorningMean))

How can i remove this empty dataframe? I already try dropna(how=’all’) but didn’t work.

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

>Solution :

You might use .empty attribute of pandas.DataFrame and if it holds true skip to next using continue loop control as follows

path = os.getcwd()
csv_files = glob.glob(os.path.join(path, "*.csv"))

for f in csv_files:
    dfDay = pd.read_csv(f, encoding = "ISO-8859-1", sep = ';')
    dfSlotMean = dfDay.groupby('slot', as_index=False)['tempo'].mean()
    slotMorningMean = dfSlotMean[dfSlotMean.slot == '7--8']
    # print(type(slotMorningMean))
    if slotMorningMean.empty:
        continue
    print((slotMorningMean))
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