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
  itertuples, datetime, pandas, groupby, in range karlito 0 234 Nov-29-2019, 11:35 AM
Last Post: karlito
  sort values of a column pandas karlito 2 296 Oct-22-2019, 06:11 AM
Last Post: karlito
  Convert df column to datetime format LarsCordes 2 294 Oct-14-2019, 12:42 PM
Last Post: LarsCordes
  Dropping a column from pandas dataframe marco_ita 6 1,010 Sep-07-2019, 08:36 AM
Last Post: marco_ita
  How to drop column in pandas SriMekala 3 509 Aug-26-2019, 06:36 PM
Last Post: snippsat
  Pandas Import CSV count between numerical values within 1 Column ptaylor520 3 267 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 580 Jun-26-2019, 11:11 PM
Last Post: Dequanharrison
  pandas change row value an existing column with conditionals Gigux 1 394 Jun-22-2019, 08:04 PM
Last Post: Gigux
  How to delete column if entire column values are "nan" Sri 4 651 Apr-13-2019, 12:16 PM
Last Post: Sri
  Text to column pandas ms5573 0 627 Aug-25-2018, 08:18 PM
Last Post: ms5573

Forum Jump:


Users browsing this thread: 1 Guest(s)