简体   繁体   中英

Pandas, Dataframe, conditional sum of column for each row

I am new to python and trying to move some of my work from excel to python, and wanted an excel SUMIFS equivalent in pandas, for example something like:

SUMIFS(F:F, D:D, "<="&C2, B:B, B2, F:F, ">"&0)

I my case, I have 6 columns, a unique Trade ID, an Issuer, a Trade date, a release date, a trader, and a quantity. I wanted to get a column which show the sum of available quantity for release at each row. Something like the below:

A   B      C         D           E            F         G
ID  Issuer TradeDate ReleaseDate Trader       Quantity  SumOfAvailableRelease
1   Horse  1/1/2012  13/3/2012   Amy          7         0
2   Horse  2/2/2012  15/5/2012   Dave         2         0
3   Horse  14/3/2012 NaN         Dave         -3        7
4   Horse  16/5/2012 NaN         John         -4        9
5   Horse  20/5/2012 10/6/2012   John         2         9
6   Fish   6/6/2013  20/6/2013   John         11        0
7   Fish   25/6/2013 9/9/2013    Amy          4         11
8   Fish   8/8/2013  15/9/2013   Dave         5         11
9   Fish   25/9/2013 NaN         Amy          -3        20

Usually, in excel, I just pull the SUMIFS formulas down the whole column and it will work, I am not sure how I can do it in python.

Many thanks!

What you could do is a df.where

so for example you could say Qdf = df.where(df["Quantity"]>=5) and then do you sum, Idk what you want to do since I have 0 knowledge about excell but I hope this helps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM