Set a column as Column Index Pandas Dataframe [duplicate]

 ̄綄美尐妖づ 提交于 2021-01-07 02:40:01

问题


Let's say I have this data set and for analysing the trends between male and female literacy across rural and urban region of every state . I need to set index as Name Which I can do as -

df.set_index('Name',inplace=True)

Now I want to segregate my data into Rural and Urban And expect the ouput as -

How can I acheive this ?

EDIT: I tried doing it using groupby

Literacy_States=Literacy_States.groupby(['Name','TRU'])['M_LIT','F_LIT'].count().unstack('TRU').plot.bar()

But ouput is :


回答1:


So what you can do is use a conditional statement to check whether TRU is rural or urban. Then throw that into a data structure- a list would do which would also make it easy to plot. Let me know if you have any questions or need further clarification!




回答2:


You can have a look here: Matplotlib bar chart that displays the x values for male and females

Instead of Male and Females, you have: Rural and Urban.



来源:https://stackoverflow.com/questions/65083775/set-a-column-as-column-index-pandas-dataframe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!