问题
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