Reputation: 379
I have a df as the following
email | date | type
_________________________
[email protected] | 6/1 | order
[email protected] | 6/1 | return
[email protected] | 6/2 | return
[email protected] | 6/2 | return
I'm trying to individualize the type of column into each row keeping the data
email | date | order | return
_________________________________
[email protected] | 6/1 | 1 | 0
[email protected] | 6/1 | 0 | 1
[email protected] | 6/2 | 0 | 0
[email protected] | 6/2 | 0 | 0
I've been trying to use pd.melt
but the output doesn't seem to be what i'm looking for. referenced from Pandas dataframe transpose with original row and column values
Upvotes: 2
Views: 755
Reputation: 241
You should have a look at how to create dummy variables from categorical columns.
There is a nice Pandas function to achieve that named "get_dummies":
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.get_dummies.html
df.drop('type', 1).join(pd.get_dummies(df['type']))
email date order return
0 [email protected] 6/1 1 0
1 [email protected] 6/1 0 1
2 [email protected] 6/2 0 1
3 [email protected] 6/2 0 1
Upvotes: 3