Below is my df:
In [78]: df
Out[78]:
site date race count
0 1 1999-01-31 Asian 100
1 1 1999-01-31 African 25
2 2 1999-01-31 Asian 200
3 1 2001-01-21 Asian 95
4 2 2001-01-21 Asian 130
5 1 2003-01-12 Asian 80
6 2 2003-01-12 Mexican 35
I want to group the above on race and date and create an output like below:
Expected:
{
"dates":[
"1999-01-31",
"2001-01-21",
"2003-01-12"
]
},
{
"race": "Asian"
"data": [
300,
225,
80
]
},
{
"race": "African"
"data": [
25,
0,
0
]
},
{
"race": "Mexican"
"data": [
0,
0,
35
]
}
My attempt:
In [77]: df.groupby(['race', 'date'])['count'].sum().reset_index(level=1)
Out[77]:
date count
race
African 1999-01-31 25
Asian 1999-01-31 300
Asian 2001-01-21 225
Asian 2003-01-12 80
Mexican 2003-01-12 35
I could groupby to get the above, but not sure how to create my expected output.