Home > database >  Remove Empty DataFrame in pandas
Remove Empty DataFrame in pandas

Time:02-21

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.

CodePudding user response:

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