0

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
Hav31ock
  • 51
  • 1
  • 8

1 Answers1

0

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
NYC Coder
  • 7,424
  • 2
  • 11
  • 24