37

I have a data frame called followers_df as below:

 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,

Jin-Dominique
  • 3,043
  • 6
  • 19
  • 28
  • Specifically, you can look at [this answer](https://stackoverflow.com/a/54297213/4909087) on how to set the index from a column or arbitrary iterable. – cs95 Jan 24 '19 at 10:08

3 Answers3

73

you can do

followers_df.index = range(20)
Roman Pekar
  • 107,110
  • 28
  • 195
  • 197
24
followers_df.reset_index()
followers_df.reindex(index=range(0,20))
yemu
  • 26,249
  • 10
  • 32
  • 29
  • 2
    I see that your answer is more appreciated, is there something wrong with assigning the index like in my answer? – Roman Pekar Oct 26 '13 at 19:18
  • Hello, for some reason I am keep getting the same error `Shape of passed values is (1, 39), indices imply (1, 20)` after trying your code....for your reference, I used `pandas.concat()` to formulate followers_df. how can I solve this issue? – Jin-Dominique Oct 26 '13 at 19:32
  • you have duplicates (the number 0), you can't reindex with duplicates, reset_index first – Jeff Oct 26 '13 at 19:39
21

When you are not sure of the number of rows, then you can do it this way:

followers_df.index = range(len(followers_df))
Taylan Aydinli
  • 4,333
  • 15
  • 39
  • 33
Shivpe_R
  • 1,022
  • 2
  • 20
  • 31