I have the data in this format, and I want to turn it around in an horizontal shape, and in the same time accumulate the count of one product, when the date and hour data are the same.
I put below the new desired data frame. Is this feasible using pandas? Or any other python library?
JavaScript
x
19
19
1
id, date, hour, name, count
2
1, 01-20, 6, car, 4
3
2, 01-20, 6, car, 3
4
3, 01-20, 7, car, 4
5
4, 01-20, 7, car, 2
6
5, 01-21, 6, car, 1
7
6, 01-21, 6, car, 1
8
7, 01-21, 7, boat, 7
9
8, 01-21, 7, boat, 8
10
9, 01-22, 6, car, 10
11
10, 01-22, 7, boat, 11
12
13
14
15
01-20(6) 01-20(7) 01-21(6) 01-21(7) 01-22(6) 01-22(7)
16
car 4+3=7 4+2=6 1+1=2 0 10 0
17
boat 0 0 0 7+8=15 0 11
18
19
Advertisement
Answer
You could groupby
+sum
, then unstack
, finally rework the column names:
JavaScript
1
6
1
df2 = (df.groupby(['date', 'hour', 'name'])
2
['count'].sum()
3
.unstack(['date', 'hour'], fill_value=0)
4
)
5
df2.columns = df2.columns.map(lambda x: f'{x[0]}({x[1]})')
6
Output:
JavaScript
1
5
1
01-20(6) 01-20(7) 01-21(6) 01-21(7) 01-22(6) 01-22(7)
2
name
3
boat 0 0 0 15 0 11
4
car 7 6 2 0 10 0
5