[英]Pandas groupby and pivot table plotting
I have a data set as such and I want to plot a bar graph showing popluation differences between rural and urban
states of all states mentioned.我有一个这样的数据集,我想 plot 条形图显示所有提到的州的
rural and urban
州之间的人口差异。
I tried using groupby -我尝试使用 groupby -
Literacy_States=Rural_Urban[['Name','TRU','M_LIT','F_LIT']]
Literacy_States=Literacy_States.groupby(['Name','TRU'])['M_LIT','F_LIT'].count().unstack('TRU').plot.bar()
But the output I get is as -但是我得到的 output 是-
Please help me out with this code.请帮我解决这个代码。 What I expect is something like-
我期望的是类似的东西 -
With male female
replaced with Rural Urban
以
male female
换Rural Urban
Link to the data set- https://github.com/Irene-123/Data-sets/blob/main/state_dist_sc.xls链接到数据集 - https://github.com/Irene-123/Data-sets/blob/main/state_dist_sc.xls
Remove unwanted total row from the data and get the rows which are states从数据中删除不需要的总行并获取状态行
Literacy_States = Literacy_States[(Literacy_States.TRU != 'Total') & (Literacy_States.Level == 'State')].copy()
Create a column for the total population为总人口创建一个列
Literacy_States['population'] = Literacy_States[['M_LIT', 'F_LIT']].sum(axis=1)
Plot the population in Urban and Rural based on Name column Plot 基于名称列的城乡人口
Literacy_States.groupby(['Name', 'TRU'])['population'].sum().unstack(1).plot.bar(rot=45)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.