python - 如何在非数值中使用数据透视表?
问题描述
我正在使用 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
解决方案
您可以使用数据透视表和聚合函数作为计数,将索引保持为“状态”。
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
推荐阅读
- python - 显示来自多个 .txt 文件的行(Python 脚本)
- azure - ApplicationInsights 资源的 SamplingPercentage 属性的策略 AllowedValues
- java - 如何在 Java 中使用 TinkerPop 执行分页?
- javascript - Javascript:如何自动选择 http/https 和 ws/wss?
- c# - 尝试使用 update 命令更新 SQL 数据库
- xml - 使用 xmlstarlet 将 XML 转换为 CSV
- sql-server - Spring Batch无效的对象名称'BATCH_JOB_INSTANCE'
- maven - Nexus 存储库匿名下载:错误还是功能?
- xml - 如何使用 XSLT 替换 XML 节点名称中的字符 - 更改根元素
- reactjs - 反应本机日历标记日期