python - 我收到关键错误 : 'District' for the command df. groupby( ["District"]).sum()[ ['TSP' ,'TSPExp' ]].sort_values( ['TSPExp' ], ascending=[False]).head(3)

标签 python pandas python-2.7

找出TSPFund和TSPExp资金运用的有效性。

df['percent']=df['TotExp']/df['Tot']*100
df['percent'].head(14)
df.groupby(['District']).sum()[['percent']].sort_values(['percent'], ascending=[False]).head(14)


df.groupby(["District"]).sum()[['TSP','TSPExp']].sort_values(['TSPExp'], ascending=[False]).head(3)

我希望得到以 District、TSPExp 和 TSP 作为列的表格演示文稿。 但我看到KeyError: 'District'

最佳答案

尝试打印数据的列名,我在读取 excel/csv 时经常遇到这种问题,大多数情况下数据中的列名包含前面或结尾的空格。

关于python - 我收到关键错误 : 'District' for the command df. groupby( ["District"]).sum()[ ['TSP' ,'TSPExp' ]].sort_values( ['TSPExp' ], ascending=[False]).head(3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56678823/

相关文章:

python - PEP 8 - 通过添加空格来对齐参数

python - Python 语音转文本模块或库 (2.7)

python - python中几个串联文件的编码问题?

python - 将元组分配给 pandas 数据帧的多个元素

pandas - 使用 iloc 从另一个查找数据帧

python - 为什么描述符 __get__ 可以访问对象的类型但 __set__ 不能?

python - 在 Python 中的 API 网关请求中使用 AWS Cognito 访问 token

python - 检查函数参数类型是 Pythonic 吗?

python - mysql-python-replicator binlog 特定模式的启动位置

python - 更改数据框的数据类型以使用该数据进行数据可视化的明确目的