python:更改pandas数据帧的行索引

时间:2013-10-26 17:22:44

标签: python pandas

我有一个名为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

我想将此数据框更改为以下内容:

 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

我该怎么做?我试过了:

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

但它给了我以下错误:

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

感谢,

3 个答案:

答案 0 :(得分:54)

你可以做到

followers_df.index = range(20)

答案 1 :(得分:21)

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

答案 2 :(得分:9)

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

followers_df.index = range(len(followers_df))