Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 itertuples, new column, datetime, pandas
#1
Hi,

I'm trying to select a range in a dataframe every time x <= 20 and create a new dataframe with a column which is the sum of the selected rows (negative value include) of that range and keep the last date as index. Thanks for some hints and if I'm on the right way :)

something like this --->
(Date as index)

This is what I tried so far but doesn't work


Date         x             Date         sum
2019-01-01   100           2019-01-05   353
2019-01-02   120   --->    2019-01-10   804
2019-01-03    80           2019-01-15   650
2019-01-04    48           2019-01-20   428
2019-01-05     5           ...
2019-01-06   110           ...
2019-01-07   420
2019-01-08   140
2019-01-09   126
2019-01-10     8
2019-01-11    50
2019-01-12   160   
2019-01-13   280
2019-01-14   148
2019-01-15    12
2019-01-16   190
2019-01-17   120
2019-01-18    80
2019-01-19    48
2019-01-20   -10
...
...

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

Top Page

Messages In This Thread
itertuples, new column, datetime, pandas - by karlito - Nov-23-2019, 09:12 PM

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pandas - Dynamic column aggregation based on another column theroadbacktonature 0 155 Apr-17-2020, 04:54 PM
Last Post: theroadbacktonature
  Add column to CSV using Pandas nsadams87xx 2 253 Apr-15-2020, 08:41 PM
Last Post: snippsat
  add formatted column to pandas data frame alkaline3 0 174 Mar-22-2020, 06:44 PM
Last Post: alkaline3
  itertuples, datetime, pandas, groupby, in range karlito 0 602 Nov-29-2019, 11:35 AM
Last Post: karlito
  sort values of a column pandas karlito 2 532 Oct-22-2019, 06:11 AM
Last Post: karlito
  Convert df column to datetime format LarsCordes 2 1,056 Oct-14-2019, 12:42 PM
Last Post: LarsCordes
  Dropping a column from pandas dataframe marco_ita 6 4,137 Sep-07-2019, 08:36 AM
Last Post: marco_ita
  How to drop column in pandas SriMekala 3 774 Aug-26-2019, 06:36 PM
Last Post: snippsat
  Pandas Import CSV count between numerical values within 1 Column ptaylor520 3 490 Jul-16-2019, 08:13 AM
Last Post: ptaylor520
  How to use the excel filename as a value to populate new column, using Pandas? Dequanharrison 5 1,312 Jun-26-2019, 11:11 PM
Last Post: Dequanharrison

Forum Jump:


Users browsing this thread: 1 Guest(s)