I have a pandas dataframe that looks like this:
But I need to pull out table and chair into their own columns to compare side by side like so:
so the values in amount are zero, and now appear in the new columns.
I am not sure how to unstack conditionally. I can unstack the entire 'furniture' column, but how do I do it for only specific row values?
Let us try get_dummies
df = df.join(df.furniture.where(df.furniture.isin(["table","chair"]),'amount').str.get_dummies().mul(df.pop("amount"),0))
df
Out[87]:
CID furniture amount chair table
0 1 couch 2 0 0
1 2 couch 3 0 0
2 2 chair 0 1 0
3 3 table 0 0 3
4 1 chair 0 1 0
5 4 lamp 5 0 0
6 4 chair 0 1 0
7 5 couch 2 0 0
8 2 lamp 5 0 0