如何计算数据透视表的聚合列中的计数?
import pandas as pd
from StringIO import StringIO
import numpy as np
audit_trail = """1|2|ENQ-wbrProcess.php|bus_departures|BUS_SERVICE_NO#DEPARTURE_TM|54790#01/12/2010|BOOKING_STATUS|O|L|WBRMWR|2010-12-01 12:42:32
5|0|DTO-transfer.php|bus_services|BUS_SERVICE_NO|159734|BUS_TYPE_CD||DO|PHRTD|2010-12-01 12:43:27
9|0|DTO-transfer.php|bus_services|BUS_SERVICE_NO|159734|EFFECTIVE_FROM||2010-12-02 00:00:00|PHRTD|2010-12-01 12:43:28
13|0|DTO-transfer.php|bus_services|BUS_SERVICE_NO|159734|MAX_CHANCE_SEATS||0|PHRTD|2010-12-01 12:43:28
17|0|DTO-transfer.php|bus_services|BUS_SERVICE_NO|159734|SCHEDULED_NO||15|PHRTD|2010-12-01 12:43:29
21|0|DTO-transfer.php|bus_services|BUS_SERVICE_NO|159734|TRIP_NATURE||Basic|PHRTD|2010-12-01 12:43:29
25|0|DTO-transfer.php|bus_services|BUS_SERVICE_NO|159734|PARCEL_SERVICE||N|PHRTD|2010-12-01 12:43:30
29|0|DTO-transfer.php|bus_services|BUS_SERVICE_NO|159734|TRIP_NO||S11308|PHRTD|2010-12-01 12:43:30
33|0|DTO-transfer.php|bus_services|BUS_SERVICE_NO|159734|IS_AVL_RESERVATION||N|PHRTD|2010-12-01 12:43:31
37|0|DTO-transfer.php|bus_service_seats|BUS_SERVICE_NO|159734|BUS_SERVICE_NO||159734|PHRTD|2010-12-01 12:43:32"""
col_list = ['transaction_id', 'request_id', 'table_name', 'table_unique_field', 'table_unique_value', 'field_name', 'old_value', 'new_value', 'client_id', 'client_type', 'transaction_date']
audit = pd.read_csv(StringIO(audit_trail), sep="|" , names = col_list, index_col='transaction_date' )
pd.pivot_table(audit, values='transaction_id', rows=['table_name'], cols=['table_unique_field'], aggfunc=np.sum)
结果如下:
table_unique_field bus_departures bus_service_seats bus_services
table_name
DTO-transfer.php NaN 37 152
ENQ-wbrProcess.php 1 NaN NaN
以上是正确显示transaction_id列的总和。我需要计数而不是总和。聚合函数np.count似乎不起作用。 预期结果:
table_unique_field bus_departures bus_service_seats bus_services
table_name
DTO-transfer.php NaN 1 8
ENQ-wbrProcess.php 1 NaN NaN
答案 0 :(得分:3)
使用len
或'count'
作为aggfunc
的参数确实有效:
In [11]: pd.pivot_table(audit, values='transaction_id', index=['table_name'],
columns=['table_unique_field'], aggfunc='count')
Out[11]:
table_unique_field bus_departures bus_service_seats bus_services
table_name
DTO-transfer.php NaN 1 8
ENQ-wbrProcess.php 1 NaN NaN
注意:最好使用index/columns
代替rows/cols
,因为这些已弃用,将在未来版本中删除(除非您使用的旧版pandas版本尚未引入)< / p>