Python Forum
itertuples, datetime, pandas, groupby, in range - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/Forum-Python-Coding)
+--- Forum: Data Science (https://python-forum.io/Forum-Data-Science)
+--- Thread: itertuples, datetime, pandas, groupby, in range (/Thread-itertuples-datetime-pandas-groupby-in-range)



itertuples, datetime, pandas, groupby, in range - karlito - Nov-29-2019

Hi,

I hope with these additional information someone could find time to help me with this issue.

sample date here --> file

'Date as index' (datetime.date)

As I said I'm trying to select a range in a dataframe every time x is in interval [-20 -190] and create a new dataframe with a new column which is the sum of the selected rows and keep the last "encountered" date as index

example and code nelow.
Thks

Date          x             Date           sum
2019-01-01  -524            2019-01-05    -1977
2019-01-02  -450   --->     2019-01-11    -3264
2019-01-03  -580            2019-01-15    -1306
2019-01-04  -408            2019-01-20    -2348
2019-01-05   -15           ...
2019-01-06  -780           ...
2019-01-07  -420
2019-01-08  -640
2019-01-09  -926
2019-01-10  -348
2019-01-11  -150
2019-01-12  -360   
2019-01-13  -280
2019-01-14  -648
2019-01-15   -18
2019-01-16  -890
2019-01-17  -320
2019-01-18  -280
2019-01-19  -748
2019-01-20  -110
...
...

for date in df.index.to_series().dt.date.unique():
   for row in df.itertuples():
      for i in row:
         if i in range(-20, -190):
           new_df = pd.DataFrame(columns=df.keys())
           new_df.index.name = 'Date'
           new_df ['sum'] = df.sum(axis = 0)
         continue