Python Forum
How to use bfill und groupby under conditions - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: How to use bfill und groupby under conditions (/thread-23160.html)



How to use bfill und groupby under conditions - karlito - Dec-13-2019

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



RE: How to use bfill und groupby under conditions - ibreeden - Dec-13-2019

Look sharp. Count the brackets. Look at the caret (^). It shows exactly where the syntax error is.