Issue
I have a dataframe:
ID Deal Party Commodity startdate enddate price quantity mtmvalue
---- ----- ----- --------- --------- ------- ------ -------- ---------
J1 Sell J (stock1, stock2) 01Jan23 01Feb23 10.00 10 100.00
J4 Buy J (stock1, stock2) 01Jan23 01Feb23 5.00 5 25.00
J2 Sell J (stock1, stock2) 01Jan23 01Feb23 10.00 10 100.00
J3 Buy J (stock1, stock2) 01Jan23 01Feb23 5.00 10 50.00
I need to group data together by Deal,Commodity, and startdate so that my dataframe looks like this:
ID Deal Party Commodity startdate enddate price quantity mtmvalue
---- ----- ----- --------- --------- ------- ------ -------- ---------
J1 Sell J (stock1, stock2) 01Jan23 01Feb23 10.00 10 100.00
J2 Sell J (stock1, stock2) 01Jan23 01Feb23 10.00 10 100.00
J3 Buy J (stock1, stock2) 01Jan23 01Feb23 5.00 10 50.00
J4 Buy J (stock1, stock2) 01Jan23 01Feb23 5.00 5 25.00
I am doing this which will create two groups, but I want it in one dataframe:
df.groupby(['Deal', 'Commodity', StartDate'])
How would I retain the groupings in the original dataframe?
Solution
Are you looking for sort_values
?
>>> df.sort_values(['Deal', 'Commodity', 'startdate'], ascending=False)
ID Deal Party Commodity startdate enddate price quantity mtmvalue
0 J1 Sell J (stock1, stock2) 01Jan23 01Feb23 10.0 10 100.0
2 J2 Sell J (stock1, stock2) 01Jan23 01Feb23 10.0 10 100.0
1 J4 Buy J (stock1, stock2) 01Jan23 01Feb23 5.0 5 25.0
3 J3 Buy J (stock1, stock2) 01Jan23 01Feb23 5.0 10 50.0
Note: There is not enough information here to correctly rank J3 ahead of J4.
Answered By - Corralien
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.