'How to groupby, and have 2 filters and get column value of corresponding filter in a new column in pandas?
{
'id':[1,1,2,2,2,3,3,3,3,3,4],
'status':['start','finalized','start','finalized','end','start','s1','s2','finalized','end','start']
'volume':[2,3,3,4,5,4,5,6,7,8,6]
}
I need to group_by id and get volume of 'start' and 'finalized' status of the the id. Basically how do I groupby and filter to create a new row.
outcome expecting
{
'id':[1,2,3]
'start_vol':[2,3,4]
'finalize_vol':[3,4,7]
}
Solution 1:[1]
You can filter and reshape with a pivot:
(df
.loc[df['status'].isin(['start', 'finalized'])]
.pivot('id', 'status', 'volume')
.dropna()
.add_suffix('_vol')
.rename_axis(columns=None)
.reset_index()
)
Output:
id finalized_vol start_vol
0 1 3 2
1 2 4 3
2 3 7 4
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | mozway |
