0

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.

2
  • Check for df.empty. Commented Feb 21, 2022 at 10:09
  • I tried that and it returns true for the row containing empty, but how do I delete that specific row? Commented Feb 21, 2022 at 10:14

1 Answer 1

1

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))
Sign up to request clarification or add additional context in comments.

2 Comments

I tried as you suggested but still get the same ouput as before
yeah that's work, we made the same change at the same time, thanks a lot.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.