0

I am trying to get the count of 'Yes' and 'No' in separate columns using pandas group-by. Below is my sample input code.

import pandas as pd
input_data = {'id': [1,1,1,2,2,3], 'status': ['Yes', 'No', 'Yes', 'No', 'No', 'Yes']}
df = pd.DataFrame(input_data)
df

enter image description here

Here's my expected output:

enter image description here

Where 'status_Yes' and 'status_No' are representing the counts of Yes and No for respective ids.

I tried the following code:

pd.DataFrame((df['status'] == 'Yes').groupby(df['id']).sum()).reset_index()

where the status column is showing the counts of 'Yes' only.

enter image description here

Is there any other way to get the expected out using pandas group-by.

Himanshu
  • 71
  • 9

0 Answers0