Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: changing row index of pandas data frame [duplicate]

Tags:

python

pandas

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,

like image 585
Jin-Dominique Avatar asked Oct 26 '13 17:10

Jin-Dominique


People also ask

Can index be duplicated pandas?

duplicated() function Indicate duplicate index values. Duplicated values are indicated as True values in the resulting array. Either all duplicates, all except the first, or all except the last occurrence of duplicates can be indicated.

How do I change the index of an existing data frame?

DataFrame - set_index() function The set_index() function is used to set the DataFrame index using existing columns. Set the DataFrame index (row labels) using one or more existing columns or arrays of the correct length. The index can replace the existing index or expand on it.


1 Answers

you can do

followers_df.index = range(20) 
like image 141
Roman Pekar Avatar answered Sep 25 '22 06:09

Roman Pekar