首页 > 解决方案 > 如何在非数值中使用数据透视表?

问题描述

我正在使用 Pandas 中的 Pivot 函数:

我的输入表是:

POI_Entity_ID            State
ADD_Q319_143936     Rajasthan
Polyline-Kot-2089       New Delhi 
Q111267412          Rajasthan 
EL_Q113_32573       Rajasthan 
RCE_UDZ_10979           New Delhi

我希望我的输出为:

Sate          counts of POI_Entity_ID
Rajasthan      3
New Delhi      2

标签: pythonpivot-table

解决方案


您可以使用数据透视表和聚合函数作为计数,将索引保持为“状态”。

d ={'POI_Entity_ID':  ['ADD_Q319_143936','Polyline-Kot-2089','Q111267412','EL_Q113_32573',
'RCE_UDZ_10979'], 'State':['Rajasthan', 'New Delhi' ,'Rajasthan',
'Rajasthan' ,'New Delhi']}

df=pd.DataFrame(data=d)

pivotdf=pd.pivot_table(data=df,index='State',values='POI_Entity_ID',aggfunc='count')

给你一个像这样的表:

           POI_Entity_ID
State                   
New Delhi              2
Rajasthan              3

推荐阅读