Python Forum
itertuples, new column, datetime, pandas
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:
Reply
#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?
Reply
#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.
Reply
#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? :)
Reply
#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.
Reply
#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.
Reply
#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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML Decoder pandas dataframe column mbrown009 3 962 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  pandas column percentile nuncio 7 2,383 Aug-10-2022, 04:41 AM
Last Post: nuncio
  pandas: Compute the % of the unique values in a column JaneTan 1 1,756 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  Pandas Data frame column condition check based on length of the value aditi06 1 2,655 Jul-28-2021, 11:08 AM
Last Post: jefsummers
  Pandas datetime: add timedelta ju21878436312 3 6,670 Jul-05-2021, 06:16 PM
Last Post: eddywinch82
  How to move each team row to a new column. Pandas vladiwnl 0 1,694 Jun-13-2021, 08:10 AM
Last Post: vladiwnl
  iretate over columns in df and calculate euclidean distance with one column in pandas Pit292 0 3,267 May-09-2021, 06:46 PM
Last Post: Pit292
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 2,915 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Pandas: summing columns conditional on the column labels ddd2332 0 2,075 Sep-10-2020, 05:58 PM
Last Post: ddd2332
  Pandas DataFrame and unmatched column sritsv19 0 2,989 Jul-07-2020, 12:52 PM
Last Post: sritsv19

Forum Jump:

User Panel Messages

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