Python Forum
How to use bfill und groupby under conditions
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to use bfill und groupby under conditions
#1
Hi,

I'm trying to backfill previous rows (which are filled with 0) with the mean of the last value before each serie of last 0.

see picture for a better understanding. [Image: 1ssVXsm] picture

here is the file file

here is what I tried

for row in data.itertuples():
    if row == 0:
        data['Date_Time'] = data['Date_Time'].where(data['x'].between(0, 0)).bfill()
        final_data = df.fillna(method='bfill').groupby('Date_Time', as_index=False)['x']).sum()

# Error
  File "<ipython-input-23-c1ec45568d73>", line 4
    final_data = df.fillna(method='bfill').groupby('Date_Time', as_index=False)['x']).sum()
                                                                                    ^
SyntaxError: invalid syntax
Reply
#2
Look sharp. Count the brackets. Look at the caret (^). It shows exactly where the syntax error is.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020