pandas 重新索引数据框问题 [英] Pandas reindexing data frame issue

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

问题描述

说我有以下数据框,

         A       B
0  1986-87  232131
1  1987-88  564564
2  1988-89  123125
               ...

以此类推.

我正在尝试用<myFrame>.set_index('A')重新编制索引,以便获得

I'm trying to reindex, with <myFrame>.set_index('A'), so that I get

                B
  1986-87  232131
  1987-88  564564
  1988-89  123125

但是我一直得到这个:

               B
       A       
 1986-87  232131
 1987-88  564564
 1988-89  123125

及其令人讨厌的原因,我尝试了其他重新索引方法.我不确定A实际代表什么,因为它没有出现在<myFrame>.columns<myFrame>.index中,而执行<myFrame>['B'][0]会给我232131,那么在此重新索引的数据帧中A是什么,并且如何从头开始正确地建立索引,或者摆脱在错误地重新建立索引的数据框中出现的这个奇怪的A.

and its annoying as heck cause I tried the other reindexing methods. I'm not sure what the A is actually representing because it doesn't appear in <myFrame>.columns or <myFrame>.index and doing <myFrame>['B'][0] gives me 232131, so what is A in this reindexed data frame and how can I index correctly from the beginning or get rid of this strange A in the incorrectly reindex data frame.

推荐答案

您需要重置索引的name/names属性:

You need to reset the name/names attribute of the index:

df.index.names = [None]

示例:

In [11]: df = pd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B']).set_index('A')

In [12]: df
Out[12]: 
   B
A   
1  2
3  4

In [13]: df.index.names = [None]

In [14]: df
Out[14]: 
   B
1  2
3  4

名称描述了索引,赋予了索引一些含义,还区分了索引的不同级别(在MultiIndex中).

@DSM指出,这样做后果自负,如果您想重置reset_index,则会丢失信息:

As @DSM points out, do so at your own peril, this loses info if you want to reset_index back:

In [15]: df.reset_index() # col_fill=['A', 'B'])
Out[15]: 
   index  B
0      1  2
1      3  4

但是,您可以手动填写名称:

However, you can col_fill in the names manually:

In [16]: df.reset_index(col_fill=['A'])
Out[16]: 
   A  B
0  1  2
1  3  4

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

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