简体   繁体   中英

losing values ​after pivot_table pandas

I have the following dataframe:

df.between_time('09:00', '09:05').head(10)

                     Qtd   Preço  Núm            CC           CV        Agr
Hora                                                                       
2020-01-19 09:05:00    5  4073.5  734    1618-Ideal    308-Clear   Vendedor
2020-01-19 09:05:00    5  4073.5  733    1618-Ideal   120-Genial   Vendedor
2020-01-19 09:05:00    5  4073.5  732    120-Genial   1618-Ideal   Vendedor
2020-01-19 09:05:00   10  4074.0  731  045-C Suisse  127-Tullett   Vendedor
2020-01-19 09:05:00    5  4074.0  730    120-Genial  127-Tullett   Vendedor
2020-01-19 09:05:00    5  4074.0  729  072-Bradesco  127-Tullett   Vendedor
2020-01-19 09:05:00    5  4074.0  728       008-UBS       003-XP   Vendedor
2020-01-19 09:04:59   20  4074.5  727     262-Mirae      122-BGC  Comprador
2020-01-19 09:04:59    5  4074.5  726  072-Bradesco      122-BGC   Vendedor
2020-01-19 09:04:59   35  4074.5  725       008-UBS      122-BGC   Vendedor

when trying to pivot the table, the values ​​are changed from int to float and the result value does not match the one realized:

df.between_time('09:00', '09:05').head(10).pivot_table(index = 'Preço', columns = 'Agr', values = 'Qtd')

Agr     Comprador  Vendedor
Preço                      
4073.5        NaN      5.00
4074.0        NaN      6.25
4074.5       20.0     20.00

seller column received an impossible value (6.25), since in the first dataframe there are only integer values.

how to fix this so that the columns receive the correct sum of the column Qty?

Default aggregate function in DataFrame.pivot_table is np.mean , so is necessary add aggfunc='sum' :

df1 = (df.between_time('09:00', '09:05')
         .head(10)
         .pivot_table(index = 'Preço', columns = 'Agr', values = 'Qtd', aggfunc='sum'))

Detail :

print (df.pivot_table(index = u'Preco', columns = 'Agr', values = 'Qtd', aggfunc='sum'))
Agr     Comprador  Vendedor
Preço                      
4073.5        NaN      15.0
4074.0        NaN      25.0
4074.5       20.0      40.0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM