python:更改熊猫数据框的行索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19609631/
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-19 14:10:07  来源:igfitidea点击:

python: changing row index of pandas data frame

pythonpandas

提问by Jin-Dominique

I have a data frame called followers_dfas below:

我有一个followers_df如下所示的数据框:

 followers_df

             0
0         oasikhia 
0     LEANEnergyUS
0  _johannesngwako
0     jamesbreenre
0   CaitlinFecteau
0  mantequillaFACE
0         apowersb
0       ecoprinter
0        tsdesigns
0      GreenBizDoc
0        JimHarris
0    Jmarti11Julia
0         JAslat63
0            prAna
0    GrantLundberg 
0        Jitasa_Is
0     ChoosePAWind
0  cleanpowerperks
0          WoWEorg
0      Laura_Chuck

I want to change this data frame into something like this:

我想将此数据框更改为如下所示:

 followers_df

             0
0          oasikhia 
1      LEANEnergyUS
2   _johannesngwako
3      jamesbreenre
4    CaitlinFecteau
5   mantequillaFACE
6          apowersb
7        ecoprinter
8         tsdesigns
9       GreenBizDoc
10        JimHarris
11    Jmarti11Julia
12         JAslat63
13            prAna
14    GrantLundberg 
15        Jitasa_Is
16     ChoosePAWind
17  cleanpowerperks
18          WoWEorg
19      Laura_Chuck

how can I do this? I tried:

我怎样才能做到这一点?我试过:

     index = pandas.Index(range(20))
     followers_df = pandas.DataFrame(followers_df, index=index)

but it's giving me the following error:

但它给了我以下错误:

  ValueError: Shape of passed values is (1, 39), indices imply (1, 20)

thanks,

谢谢,

采纳答案by Roman Pekar

you can do

你可以做

followers_df.index = range(20)

回答by yemu

followers_df.reset_index()
followers_df.reindex(index=range(0,20))

回答by Shivpe_R

When You are Not Sure of the Number of Rows, Then u Can Do this Way

当你不确定行数时,你可以这样做

followers_df.index = range(len(followers_df))