Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change dataframe index values while keeping other column data same

Tags:

python

pandas

I have a DataFrame with 4 columns and 251 rows and an index that is a progression of numbers e.g. 1000 to 1250 . The index was initially necessary to aid in joining data from 4 different dataframes. However, once i get the 4 columns together, i would like to change the index to a number progression from 250 to 0. This is because i would be performing the same operation on different sets of data (in groups of 4) that would have different indices, e.g. 2000 to 2250 or 500 to 750, but would all have the same number of rows. 250 to 0 is a way of unifying these data sets, but i can't figure out how to do this. i.e. i'm looking for something that replaces any existing index with the function range(250, 0, -1)

I've tried using set_index below and a whole bunch of other attempts that invariably return errors,

df.set_index(range(250, 0, -1), inplace=True) 

and in the instance when i am able to set the index of the df to the range, the data in the 4 columns change to NaN since they have no data that matches the new index. I apologize if this is rudimentary, but i'm a week old in the world of python/pandas, haven't programmed in +10yrs, and have taken 2 days to try to figure this out for myself as an exercise, but its time to cry... Uncle!!

like image 874
WittyID Avatar asked Aug 22 '13 04:08

WittyID


2 Answers

Try introducing the 250:0 indices as a column first, then setting them as the index:

df = pd.DataFrame({'col1': list('abcdefghij'), 'col2': range(0, 50, 5)})
df['new_index'] = range(30, 20, -1)
df.set_index('new_index')

Before:

  col1  col2  new_index
0    a     0         30
1    b     5         29
2    c    10         28
3    d    15         27
4    e    20         26
5    f    25         25
6    g    30         24
7    h    35         23
8    i    40         22
9    j    45         21

After:

          col1  col2
new_index           
30           a     0
29           b     5
28           c    10
27           d    15
26           e    20
25           f    25
24           g    30
23           h    35
22           i    40
21           j    45
like image 155
Marius Avatar answered Sep 20 '22 23:09

Marius


You can just do

df.index = range(250, 0, -1)

or am I missing something?

like image 32
JoeCondron Avatar answered Sep 17 '22 23:09

JoeCondron