熊猫:数据透视表

时间:2016-08-12 13:00:36

标签: python pandas dataframe unique pivot-table

我有df:

ID,url,used_at,active_seconds,domain,search_engine,diff_time,period,code, category
08cd0141663315ce71e0121e3cd8d91f,market.yandex.ru/product/12858630?hid=91491&track=fr_same,2016-03-20 23:19:49,6,yandex.ru,None,78.0,515,100.0, Search system
08cd0141663315ce71e0121e3cd8d91f,market.yandex.ru/product/12858630?hid=91491&track=fr_same,2016-03-20 23:20:01,26,yandex.ru,None,6.0,515,100.0, Social network
08cd0141663315ce71e0121e3cd8d91f,market.yandex.ru/catalog/54726/list?hid=91491&track=pieces&gfilter=1801946%3A1871375&exc=1&regprice=9&how=dpop,2016-03-20 23:20:33,14,yandex.ru,None,6.0,515,100.0, Social network
08cd0141663315ce71e0121e3cd8d91f,market.yandex.ru/product/12858630/offers?hid=91491&grhow=shop,2016-03-20 23:20:47,2,yandex.ru,None,14.0,515,100.0, Internet shop
08cd0141663315ce71e0121e3cd8d91f,market.yandex.ru/product/12858630/offers?hid=91491&grhow=shop,2016-03-20 23:24:05,8,yandex.ru,None,196.0,515,100.0, Internet shop
08cd0141663315ce71e0121e3cd8d91f,market.yandex.ru/catalogmodels.xml?hid=91491&CAT_ID=160043&nid=54726&track=pieces,2016-03-20 23:24:13,32,yandex.ru,None,8.0,515,100.0, Search system
08cd0141663315ce71e0121e3cd8d91f,market.yandex.ru/catalog/54726/list?hid=91491&track=fr_cm_shwall&exc=1&how=dpop,2016-03-20 23:24:45,16,yandex.ru,None,32.0,515,100.0, Internet shop
08cd0141663315ce71e0121e3cd8d91f,market.yandex.ru/catalogmodels.xml?hid=91491&CAT_ID=160043&nid=54726&track=pieces,2016-03-20 23:25:01,4,yandex.ru,None,16.0,515,100.0, Search system
08cd0141663315ce71e0121e3cd8d91f,market.yandex.ru/catalog/54726/list?hid=91491&track=fr_cm_pop&exc=1&how=dpop,2016-03-20 23:25:05,10,yandex.ru,None,4.0,515,100.0, Social network
08cd0141663315ce71e0121e3cd8d91f,market.yandex.ru/product/11153512?hid=91491&track=fr_same,2016-03-21 06:52:44,2,yandex.ru,None,14.0,516,100.0, Internet shop
08cd0141663315ce71e0121e3cd8d91f,market.yandex.ru/catalog/54726/list?hid=91491&track=pieces&gfilter=1801946%3A1871375&exc=1&regprice=9&how=dpop,2016-04-04 21:08:41,24,yandex.ru,None,20.0,562,100.0, Internet shop
0bc0898d3fe2e46158621c674effb458,market.yandex.ru/product/12259780?hid=91491&show-uid=56508001849064882783001,2016-02-26 20:34:20,28,yandex.ru,yandex,10.0,1217,100.0, Social network
0bc0898d3fe2e46158621c674effb458,market.yandex.ru/product/12259780?hid=91491&show-uid=56508001849064882783001,2016-02-26 20:34:50,1,yandex.ru,None,2.0,1217,100.0, Internet shop

我需要构建pivot_table。 我用

table = pd.pivot_table(df, values='domain', index=['ID'], columns=['category'], aggfunc=np.sum)

但问题在于它连接domain,但我想要计算独特域的数量。我怎么能这样做?

1 个答案:

答案 0 :(得分:2)

看起来需要:

table = pd.pivot_table(df, values='domain', 
                           index=['ID'], 
                           columns=['category'], 
                           aggfunc=lambda x: x.nunique())
print (table)

category                           Internet shop   Search system  \
ID                                                                 
08cd0141663315ce71e0121e3cd8d91f             1.0             1.0   
0bc0898d3fe2e46158621c674effb458             1.0             NaN   

category                           Social network  
ID                                                 
08cd0141663315ce71e0121e3cd8d91f              1.0  
0bc0898d3fe2e46158621c674effb458              1.0 

另一个更快的解决方案:

print (df.groupby(['ID','category'])['domain'].nunique().unstack())
category                           Internet shop   Search system  \
ID                                                                 
08cd0141663315ce71e0121e3cd8d91f             1.0             1.0   
0bc0898d3fe2e46158621c674effb458             1.0             NaN   

category                           Social network  
ID                                                 
08cd0141663315ce71e0121e3cd8d91f              1.0  
0bc0898d3fe2e46158621c674effb458              1.0