I have a two columned data set that I would like to reshape.
Looking at this fake df:
df=pd.DataFrame([
['Alex', 'Apple'],['Bob', 'Banana'],['Clark', 'Citrus'], ['Diana', 'Banana'], [
'Elisa', 'Apple'], ['Frida', 'Citrus'], ['George', 'Citrus'], ['Hanna', 'Banana']
],columns=['Name', 'Fruit'])
I would like to have four columns; Name, Apple, Banana and Citrus where the three latter are booleans (true/false).
I've looked inte unstack but it's really not what I am looking for.
I think this should be a good use case for get_dummies
:
df.set_index('Name')['Fruit'].str.get_dummies().astype(bool).reset_index()
Name Apple Banana Citrus
0 Alex True False False
1 Bob False True False
2 Clark False False True
3 Diana False True False
4 Elisa True False False
5 Frida False False True
6 George False False True
7 Hanna False True False
In similar vein, we have,
pd.concat([df['Name'], df['Fruit'].str.get_dummies().astype(bool)], axis=1)
Name Apple Banana Citrus
0 Alex True False False
1 Bob False True False
2 Clark False False True
3 Diana False True False
4 Elisa True False False
5 Frida False False True
6 George False False True
7 Hanna False True False
You can use the below:
df[['Name']].join(pd.get_dummies(df.Fruit).astype(bool))
Name Apple Banana Citrus
0 Alex True False False
1 Bob False True False
2 Clark False False True
3 Diana False True False
4 Elisa True False False
5 Frida False False True
6 George False False True
7 Hanna False True False
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With