0

So Basically here is a simplified version of my dataframe, and the 2. picture is what I want to get. : https://i.stack.imgur.com/nerDf.jpg

An explanation: Basically the 20201001 stuff is the date in a number format, and I want to group up the values for each date for some Group and Name.

Here comes my issue: I tried using df.groupby(by=['Credit','Equity','Bond').sum but It grouped everything up, not only the ones in the list (there are much more in the original dataset which I dont want to group up.

The second issue is that there are 2 things which group up in a different row (Stock and Option) so not sure how I could do that with pandas.

In excel I just got the result by a simple SUMIF function.

JohnFire
  • 115
  • 5
  • Please share sample input data with expected output. – Mayank Porwal Nov 26 '20 at 16:57
  • Either `df[list_of_columns].groupby(...)` to only operate on the subset of `df` defined by `list_of_columns`, or `df.groupby(by=list_of_columns, ...)` to groupby according to `list_of_columns`. To be more coherent, we need to see a more coherent example from you (OP). – itaishz Nov 26 '20 at 17:02
  • Thank you, thats exactly what I needed @itaishz the problem is its a work file, would be almost impossible to recreate it – JohnFire Nov 26 '20 at 17:04
  • @itaishz I did the df.groupby(by = ['y','x']...) but it still groups up everything same as before – JohnFire Nov 26 '20 at 17:08
  • @JohnFire you really should provide some input/output... I'm not for instance sure if you even want a groupby? Maybe just `df[['a', 'b', 'c']].sum(axis=1)` or something - it's confusing why you've got a `level` and `axis` there etc... if you could elaborate a bit and provide an example - I'm sure someone would be able to help – Jon Clements Nov 26 '20 at 17:13
  • @JonClements I will try to make a simpler version of it and post the question like that, I'm not sure either, someone helped with the code in a very simplified version of the file – JohnFire Nov 26 '20 at 17:16
  • @JohnFire thanks... if you can do that and make an [edit] to your Q - that'd be great – Jon Clements Nov 26 '20 at 17:17
  • @JonClements done – JohnFire Nov 26 '20 at 17:30
  • @JohnFire great attempt - thanks... I should have pointed you towards https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples - would you mind taking a look at that and seeing if you can do something more like that please? – Jon Clements Nov 26 '20 at 17:39
  • @JonClements I can try, but will that actually help ppl with answering? I think everything is visible on the imgur link – JohnFire Nov 26 '20 at 17:59
  • @JohnFire it'd definitely help... with just an image - someone would have to manually make that into something they can actually work with and most people aren't going to bother with that... – Jon Clements Nov 26 '20 at 18:30

0 Answers0