How To Create New Columns From Subcategories In Pandas?
I'm trying to take subcategories and place them into columns so I can compute values for each column. For example, what I have now is: c1 c2 c3 0 123 Orange 12 1 123 C
Solution 1:
Something like this will do the trick for you:
First if you need breakdown per Colour
/Transport
- you need to classify it accordingly, so:
>>> df
c1 c2 c3 c4
0123 Orange 12 Colour
1123 Car 15 Transport
2123 Blue 14 Colour
3123 Bike 13 Transport
4234 Red 9 Colour
5234 Bus 4 Transport
6234 Train 19 Transport
7234 Purple 17 Colour
Then in order to get exactly what you want (so kind of aggregation with "sumif"):
>>> df.assign(c3_Colour=df["c3"][df["c4"]=="Colour"], c3_Transport=df["c3"][df["c4"]=="Transport"]).fillna(0).groupby(c1).agg({"c3_Colour":sum, "c3_Transport": sum})
c3_Colour c3_Transport
12326.028.023426.023.0
Post a Comment for "How To Create New Columns From Subcategories In Pandas?"