将列设置为列索引 pandas 数据框 [英] Set a column as Column Index Pandas Dataframe

查看:62
本文介绍了将列设置为列索引 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我拥有此数据集,并用于分析每个州城乡地区男女识字率之间的趋势.我需要将索引设置为 Name 我可以做的-

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

现在我想将我的数据分为 Rural和Urban 并期望输出为-

我该如何实现?

我尝试使用 groupby

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

但是输出是:

解决方案

您可以在这里查看: Matplotlib条形图,显示了x值对于男性和女性

您将拥有:农村和城市,而不是男性和女性.

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 :

解决方案

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.

这篇关于将列设置为列索引 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆