Reputation: 69
I am very novice Python pandas user so this may be a dumb question and pandas may have simple method to do this operation
My data frame is as follows:
Name Activity Date
Abc. Run. June 3
Cde. Walk. Sept. 4
Abc. Run. June 4
Abc. Run. June 5
Code. Walk. June 3
Cde. Run. Sept 5
I want to convert it to
Name run freq walk freq
Abc. 3. 0
Cde. 1. 1
Code. 0. 1
How do I do this in pandas?
Upvotes: 1
Views: 47
Reputation: 109526
Group on the column, get the count of each, and then unstack the results.
df = pd.DataFrame({'Name': ['Abc.', 'Cde.', 'Abc.', 'Abc.', 'Code.', 'Cde.'],
'Activity': ['Run.', 'Walk.', 'Run.', 'Run.', 'Walk.', 'Run.'],
'Date': ['June 3', 'Sept. 4', 'June 4', 'June 5', 'June 3', 'Sept 5']})
>>> df.groupby(['Name', 'Activity']).count().unstack()
Date
Activity Run. Walk.
Name
Abc. 3 NaN
Cde. 1 1
Code. NaN 1
Upvotes: 1
Reputation: 5467
You want to group by name and activity and get the sum:
df.groupby(['Name', 'Activity']).sum()
Upvotes: 1