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
#2
Hi, is date the index and in your sum example, the first sum should be 353 not 343?
In your example the sequence length is always 5, is that guaranteed?
Quote
#3
(Nov-24-2019, 10:28 AM)ThomasL Wrote: Hi, is date the index and in your sum example, the first sum should be 353 not 343?
In your example the sequence length is always 5, is that guaranteed?

Yes Date is already set as index and yes it should be 353 sorry typo. NO the sequence is not guaranteed because it changed over time. Thks for pointing that.
Quote
#4
(Nov-24-2019, 10:28 AM)ThomasL Wrote: Hi, is date the index and in your sum example, the first sum should be 353 not 343?
In your example the sequence length is always 5, is that guaranteed?

Hi,
I thought you asked because you wanted to help? :)
Quote
#5
Hi, sorry, i wasn´t able to solve your problem in my spare time.
My pandas knowledge isn´t good enough.
Maybe have a look at Kevin Markhams youtube videos and/or his website.
Quote
#6
(Nov-28-2019, 03:43 PM)karlito Wrote: I thought you asked because you wanted to help? :)

Did you provide sample data in easy to use (copy-paste) format?

Did you provided enough information ('Date as index' - is it datetime.date or what?, do you want select or sum all values less than 20?, are all errors and typos in sample eliminated?).

If one makes helping as hard as possible then it significantly reduces possibilities that someone is ready to invest time and effort into it.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Quote
#7
(Nov-29-2019, 06:51 AM)perfringo Wrote:
(Nov-28-2019, 03:43 PM)karlito Wrote: I thought you asked because you wanted to help? :)

Did you provide sample data in easy to use (copy-paste) format?

Did you provided enough information ('Date as index' - is it datetime.date or what?, do you want select or sum all values less than 20?, are all errors and typos in sample eliminated?).

If one makes helping as hard as possible then it significantly reduces possibilities that someone is ready to invest time and effort into it.

Oh sorry about that! I will modify the original post and then add additional infos.
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Pandas DataFrame and unmatched column sritsv19 0 130 Jul-07-2020, 12:52 PM
Last Post: sritsv19
  Assigning Column nunique values to another DataFrame column Pythonito 0 134 Jun-25-2020, 05:04 PM
Last Post: Pythonito
  Pandas - Dynamic column aggregation based on another column theroadbacktonature 0 209 Apr-17-2020, 04:54 PM
Last Post: theroadbacktonature
  Add column to CSV using Pandas nsadams87xx 2 335 Apr-15-2020, 08:41 PM
Last Post: snippsat
  add formatted column to pandas data frame alkaline3 0 232 Mar-22-2020, 06:44 PM
Last Post: alkaline3
  itertuples, datetime, pandas, groupby, in range karlito 0 659 Nov-29-2019, 11:35 AM
Last Post: karlito
  sort values of a column pandas karlito 2 583 Oct-22-2019, 06:11 AM
Last Post: karlito
  Convert df column to datetime format LarsCordes 2 1,338 Oct-14-2019, 12:42 PM
Last Post: LarsCordes
  Dropping a column from pandas dataframe marco_ita 6 4,696 Sep-07-2019, 08:36 AM
Last Post: marco_ita
  How to drop column in pandas SriMekala 3 844 Aug-26-2019, 06:36 PM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)