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


Messages In This Thread
itertuples, datetime, pandas, groupby, in range - by karlito - Nov-29-2019, 11:35 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pandas datetime: add timedelta ju21878436312 3 6,670 Jul-05-2021, 06:16 PM
Last Post: eddywinch82
  Remove extra count columns created by pandas groupby spyf8 1 2,686 Feb-10-2021, 09:19 AM
Last Post: Naheed
  Combine groupby() and shift() in pandas rama27 0 4,152 Nov-17-2020, 09:49 PM
Last Post: rama27
  Cycle through Numpy range within another range(?) Zero01 0 1,993 Jul-31-2020, 02:37 PM
Last Post: Zero01
  Pandas + Groupby + Filter unique values JosepMaria 1 2,839 Jun-15-2020, 08:15 AM
Last Post: JosepMaria
  python pandas: diff between 2 dates in a groupby bluedragon 0 3,250 Mar-25-2020, 04:18 PM
Last Post: bluedragon
  pandas.read_sas with chunksize: IndexError list index out of range axelle 0 2,549 Jan-28-2020, 09:30 AM
Last Post: axelle
  itertuples, new column, datetime, pandas karlito 6 3,889 Nov-29-2019, 11:07 AM
Last Post: karlito
  Groupby in pandas with conditional - add and subtract rregorr 2 6,917 Jul-12-2019, 05:17 PM
Last Post: rregorr
  Pandas segmenting groupby average brocq_18 0 2,407 Jul-11-2018, 10:54 AM
Last Post: brocq_18

Forum Jump:

User Panel Messages

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