Given the fictional data frame:
Fruit Seeds
Apple Yes
Banana No
Pear Yes
Apple Yes
Banana No
How can I transform it to the following?
Fruit Yes No Total
Apple 2 0 2
Banana 0 2 2
Pear 1 0 1
Given the fictional data frame:
Fruit Seeds
Apple Yes
Banana No
Pear Yes
Apple Yes
Banana No
How can I transform it to the following?
Fruit Yes No Total
Apple 2 0 2
Banana 0 2 2
Pear 1 0 1
Try this:
df = pd.crosstab(df['Fruit'], df['Seeds'])
df['Total'] = df['Yes'] + df['No']
print(df.reset_index())
Seeds Fruit No Yes Total
0 Apple 0 2 2
1 Banana 2 0 2
2 Pear 0 1 1