Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Pivot Table margins=True不能很好地求和_Python_Pandas_Pivot Table - Fatal编程技术网

Python Pivot Table margins=True不能很好地求和

Python Pivot Table margins=True不能很好地求和,python,pandas,pivot-table,Python,Pandas,Pivot Table,我有以下代码: import pandas as pd df=pd.read_csv("https://www.dropbox.com/s/90y07129zn351z9/test_data.csv?dl=1", encoding="latin-1") pvt_received=df.pivot_table(index=['site'], values = ['received','sent'], aggfunc = { 'received' : 'count' ,'sent': 'count

我有以下代码:

import pandas as pd
df=pd.read_csv("https://www.dropbox.com/s/90y07129zn351z9/test_data.csv?dl=1", encoding="latin-1")
pvt_received=df.pivot_table(index=['site'], values = ['received','sent'], aggfunc = {  'received' : 'count' ,'sent': 'count'}, fill_value=0, margins=True) 
pvt_received['to_send']=pvt_received['received']-pvt_received['sent']
column_order = ['received', 'sent','to_send']
pvt_received_ordered = pvt_received.reindex_axis(column_order, axis=1)
pvt_received_ordered.to_csv("test_pivot.csv")
table_to_send = pd.read_csv('test_pivot.csv', encoding='latin-1')
table_to_send.rename(columns={'site':'Site','received':'Date Received','sent':'Date Sent','to_send':'Date To Send'}, inplace=True)
table_to_send.set_index('Site', inplace=True)
table_to_send
将生成此表:

      Date Received       Date Sent       Date To Send
Site            
2         32.0             27.0           5.0
3         20.0             17.0           3.0
4         33.0             31.0           2.0
5         40.0             31.0           9.0
All       106.0            106.0          0.0

但此参数margins=True不能给出每列总和的正确结果。例如,接收日期应为125而不是106,发送日期应为106(正确),发送日期应为19而不是0.0(零)。问题:我应该改变什么才能得到正确的数字?此外,还缺少对所有行进行求和的功能。提前非常感谢。

从您的代码中可以看出,在构建透视表之后,您创建了要发送的日期,因此它只是给您以下结果:
106.0-106.0
。此外,分组后的边距值默认为
dropna=True
,这意味着任何带有
NaN
NaT
的行都将被删除。设置
dropna=False应该可以解决此问题

我重构了您的代码,将
接收的
发送的
列转换为
日期时间
格式,然后创建透视表和
发送的

df2 = pd.read_csv(
         "https://www.dropbox.com/s/90y07129zn351z9/test_data.csv?dl=1"
         ,encoding="latin-1")
df2['received'] = pd.to_datetime(df2['received'])
df2['sent'] = pd.to_datetime(df2['sent'])
然后创建最初计划的透视表

pvt_received = df2.pivot_table(index=['site'], values=['received','sent'],\
    aggfunc='count', margins=True, dropna=False)

pvt_received['to_send'] = pvt_received['received'] - pvt_received['sent']
pvt_received.rename(columns={'site':'Site'
                             ,'received':'Date Received'
                             ,'sent':'Date Sent'
                             ,'to_send':'Date To Send'}
                             ,inplace=True)
pvt_received

        Date Received   Date Sent   Date To Send
Site            
2       32              27          5
3       20              17          3
4       33              31          2
5       40              31          9
All     125             106         25

从至发送的值错误,发送日期应为19而不是106。