Python Forum
itertuples, datetime, pandas, groupby, in range
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
itertuples, datetime, pandas, groupby, in range
#1
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas datetime: add timedelta ju21878436312 1 190 Feb-25-2021, 04:26 PM
Last Post: eddywinch82
  Remove extra count columns created by pandas groupby spyf8 1 224 Feb-10-2021, 09:19 AM
Last Post: Naheed
  Combine groupby() and shift() in pandas rama27 0 368 Nov-17-2020, 09:49 PM
Last Post: rama27
  Cycle through Numpy range within another range(?) Zero01 0 490 Jul-31-2020, 02:37 PM
Last Post: Zero01
  Pandas + Groupby + Filter unique values JosepMaria 1 577 Jun-15-2020, 08:15 AM
Last Post: JosepMaria
  python pandas: diff between 2 dates in a groupby bluedragon 0 1,599 Mar-25-2020, 04:18 PM
Last Post: bluedragon
  pandas.read_sas with chunksize: IndexError list index out of range axelle 0 705 Jan-28-2020, 09:30 AM
Last Post: axelle
  itertuples, new column, datetime, pandas karlito 6 1,131 Nov-29-2019, 11:07 AM
Last Post: karlito
  Groupby([]).sum() Miscalculation mini_geek 1 724 Sep-16-2019, 12:43 AM
Last Post: Larz60+
  Groupby in pandas with conditional - add and subtract rregorr 2 3,867 Jul-12-2019, 05:17 PM
Last Post: rregorr

Forum Jump:

User Panel Messages

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