Here’s the data in pandas dataframe:
Hour | Country |
---|---|
1 | Indonesia |
1 | USA |
2 | India |
3 | Indonesia |
3 | Indonesia |
I want to aggregate them like this:
hourcountry | Indonesia | USA | India |
---|---|---|---|
1 | 1 | 1 | 0 |
2 | 0 | 0 | 1 |
3 | 2 | 0 | 0 |
I tried to do this:
df.groupby(['hour','country']).count()
but it turned into this:
hour | country | count |
---|---|---|
1 | India | 0 |
USA | 1 | |
Indonesia | 1 | |
2 | India | 1 |
USA | 0 | |
Indonesia | 0 | |
3 | India | 0 |
USA | 0 | |
Indonesia | 2 |
How to make the unique values of the country into column instead?
New contributor
user15587046 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.