Skip to content
Advertisement

Pandas pivot value are merged

Hi have data like below

Time                  qty
2021-07-23 02-06-49  0.02
2021-07-23 02-06-51  0.02
2021-07-23 02-06-53  2
2021-07-23 02-06-53  0.032
2021-07-23 02-06-54  2.842
2021-07-23 02-06-54  0.025
2021-07-23 02-06-55  0.02
2021-07-23 02-06-57  1.742
2021-07-23 02-06-57  0.395
df = pd.pivot_table(df,index=['Time'],values=['qty'],aggfunc=np.sum)

print(df)

result like below

Time                    qty
2021-07-23 02-06-49     0.02
2021-07-23 02-06-51     0.02
2021-07-23 02-06-53     2.000000000.03200000
2021-07-23 02-06-54     2.842000000.02500000
2021-07-23 02-06-55     0.02
2021-07-23 02-06-57     1.742000000.39500000

I want the sum of qty value

Advertisement

Answer

Based on the output, it seems to me that “qty” a “string” dtype. Try forcing it to be a “float” and try again:

df = df.astype({"qty": "float"})
df = pd.pivot_table(df, index=['Time'], values=['qty'], aggfunc=np.sum)
Advertisement