How Can Create Counts Of Terms In A One Column And Abend The Counts As Additional Coulmns In Pandas Data Frame
I have a panda data frame that looks like this: ID Key 101 A 102 A 205 A 101 B 105 A 605 A 200 A 102 B I would like to make a new table that counts the number of occurre
Solution 1:
You are close, need unstack
:
df = df.groupby(['ID', 'Key']).size().unstack(fill_value=0)
print (df)
Key A B
ID
101 1 1
102 1 1
105 1 0
200 1 0
205 1 0
605 1 0
Or crosstab
:
df = pd.crosstab(df['ID'], df['Key'])
print (df)
Key A B
ID
101 1 1
102 1 1
105 1 0
200 1 0
205 1 0
605 1 0
Post a Comment for "How Can Create Counts Of Terms In A One Column And Abend The Counts As Additional Coulmns In Pandas Data Frame"