Bottom Page

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
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Code improvement: groupby and operation on conditionals pythonidae 5 202 Yesterday, 01:57 PM
Last Post: pythonidae
  itertuples, new column, datetime, pandas karlito 6 212 Nov-29-2019, 11:07 AM
Last Post: karlito
  Groupby([]).sum() Miscalculation mini_geek 1 245 Sep-16-2019, 12:43 AM
Last Post: Larz60+
  Groupby in pandas with conditional - add and subtract rregorr 2 1,045 Jul-12-2019, 05:17 PM
Last Post: rregorr
  Pandas segmenting groupby average brocq_18 0 591 Jul-11-2018, 10:54 AM
Last Post: brocq_18
  Finding date count from a list of date range in pandas trillerducas72 0 697 May-24-2018, 02:30 AM
Last Post: trillerducas72
  groupby question smw10c 2 1,335 Mar-29-2017, 11:17 PM
Last Post: smw10c
  Python Pandas datetime nuncio 3 6,238 Jan-08-2017, 09:34 AM
Last Post: nuncio
  is a pandas dataframe timeseries time index in a specified range (but ignoring date)? m_lotinga 4 13,592 Dec-12-2016, 10:51 PM
Last Post: m_lotinga

Forum Jump:


Users browsing this thread: 1 Guest(s)