Python 如何将熊猫数据框的索引转换为列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20461165/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 20:30:06  来源:igfitidea点击:

How to convert index of a pandas dataframe into a column?

pythonpandasdataframe

提问by msakya

This seems rather obvious, but I can't seem to figure out how to convert an index of data frame to a column?

这似乎很明显,但我似乎无法弄清楚如何将数据框的索引转换为列?

For example:

例如:

df=
        gi       ptt_loc
 0  384444683      593  
 1  384444684      594 
 2  384444686      596  

To,

到,

df=
    index1    gi       ptt_loc
 0  0     384444683      593  
 1  1     384444684      594 
 2  2     384444686      596  

采纳答案by behzad.nouri

either:

任何一个:

df['index1'] = df.index

or, .reset_index:

或者,.reset_index

df.reset_index(level=0, inplace=True)


so, if you have a multi-index frame with 3 levels of index, like:

因此,如果您有一个具有 3 个索引级别的多索引框架,例如:

>>> df
                       val
tick       tag obs        
2016-02-26 C   2    0.0139
2016-02-27 A   2    0.5577
2016-02-28 C   6    0.0303

and you want to convert the 1st (tick) and 3rd (obs) levels in the index into columns, you would do:

并且您想将索引中的第 1 ( tick) 和第 3 ( obs) 级转换为列,您可以这样做:

>>> df.reset_index(level=['tick', 'obs'])
          tick  obs     val
tag                        
C   2016-02-26    2  0.0139
A   2016-02-27    2  0.5577
C   2016-02-28    6  0.0303

回答by Apogentus

For MultiIndex you can extract its subindex using

对于 MultiIndex,您可以使用提取其子索引

df['si_name'] = R.index.get_level_values('si_name') 

where si_nameis the name of the subindex.

其中si_name是子索引的名称。

回答by Ted Petrou

To provide a bit more clarity, let's look at a DataFrame with two levels in its index (a MultiIndex).

为了更清晰,让我们看一下索引中有两个级别(MultiIndex)的 DataFrame。

index = pd.MultiIndex.from_product([['TX', 'FL', 'CA'], 
                                    ['North', 'South']], 
                                   names=['State', 'Direction'])

df = pd.DataFrame(index=index, 
                  data=np.random.randint(0, 10, (6,4)), 
                  columns=list('abcd'))

enter image description here

在此处输入图片说明

The reset_indexmethod, called with the default parameters, converts all index levels to columns and uses a simple RangeIndexas new index.

reset_index方法使用默认参数调用,将所有索引级别转换为列并使用简单RangeIndex索引作为新索引。

df.reset_index()

enter image description here

在此处输入图片说明

Use the levelparameter to control which index levels are converted into columns. If possible, use the level name, which is more explicit. If there are no level names, you can refer to each level by its integer location, which begin at 0 from the outside. You can use a scalar value here or a list of all the indexes you would like to reset.

使用该level参数来控制将哪些索引级别转换为列。如果可能,请使用更明确的级别名称。如果没有级别名称,您可以通过其整数位置来引用每个级别,该位置从外部从 0 开始。您可以在此处使用标量值或要重置的所有索引的列表。

df.reset_index(level='State') # same as df.reset_index(level=0)

enter image description here

在此处输入图片说明

In the rare event that you want to preserve the index and turn the index into a column, you can do the following:

在极少数情况下,您希望保留索引并将索引转换为列,您可以执行以下操作:

# for a single level
df.assign(State=df.index.get_level_values('State'))

# for all levels
df.assign(**df.index.to_frame())

回答by bunji

If you want to use the reset_indexmethod and also preserve your existing index you should use:

如果要使用该reset_index方法并保留现有索引,则应使用:

df.reset_index().set_index('index', drop=False)

or to change it in place:

或将其更改到位:

df.reset_index(inplace=True)
df.set_index('index', drop=False, inplace=True)

For example:

例如:

print(df)
          gi  ptt_loc
0  384444683      593
4  384444684      594
9  384444686      596

print(df.reset_index())
   index         gi  ptt_loc
0      0  384444683      593
1      4  384444684      594
2      9  384444686      596

print(df.reset_index().set_index('index', drop=False))
       index         gi  ptt_loc
index
0          0  384444683      593
4          4  384444684      594
9          9  384444686      596

And if you want to get rid of the index label you can do:

如果你想摆脱索引标签,你可以这样做:

df2 = df.reset_index().set_index('index', drop=False)
df2.index.name = None
print(df2)
   index         gi  ptt_loc
0      0  384444683      593
4      4  384444684      594
9      9  384444686      596

回答by jpp

rename_axis+ reset_index

rename_axis+ reset_index

You can first rename your index to a desired label, thenelevate to a series:

您可以先将索引重命名为所需的标签,然后提升为系列:

df = df.rename_axis('index1').reset_index()

print(df)

   index1         gi  ptt_loc
0       0  384444683      593
1       1  384444684      594
2       2  384444686      596

This works also for MultiIndexdataframes:

这也适用于MultiIndex数据框:

print(df)
#                        val
# tick       tag obs        
# 2016-02-26 C   2    0.0139
# 2016-02-27 A   2    0.5577
# 2016-02-28 C   6    0.0303

df = df.rename_axis(['index1', 'index2', 'index3']).reset_index()

print(df)

       index1 index2  index3     val
0  2016-02-26      C       2  0.0139
1  2016-02-27      A       2  0.5577
2  2016-02-28      C       6  0.0303

回答by Avneesh Hota

df1 = pd.DataFrame({"gi":[232,66,34,43],"ptt":[342,56,662,123]})
p = df1.index.values
df1.insert( 0, column="new",value = p)
df1

    new     gi     ptt
0    0      232    342
1    1      66     56 
2    2      34     662
3    3      43     123

回答by maria_g

A very simple way of doing this is to use reset_index() method.For a data frame df use the code below:

一个非常简单的方法是使用 reset_index() 方法。对于数据框 df 使用以下代码:

df.reset_index(inplace=True)

This way, the index will become a column, and by using inplace as True,this become permanent change.

这样,索引将成为一列,并且通过使用 inplace 为 True,这将成为永久更改。