找出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/