简体   繁体   English

在pivot_table pandas之后丢失值

[英]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:尝试旋转表时,值从 int 更改为 float 并且结果值与实现的值不匹配:

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.卖家列收到一个不可能的值 (6.25),因为在第一个数据帧中只有整数值。

how to fix this so that the columns receive the correct sum of the column Qty?如何解决这个问题,以便列收到列 Qty 的正确总和?

Default aggregate function in DataFrame.pivot_table is np.mean , so is necessary add aggfunc='sum' : DataFrame.pivot_table默认聚合函数是np.mean ,因此有必要添加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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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