Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set absolutely new index to pandas DataFrame, not based on previous index-data relation?

How can I assign an absolutely new index to my data without any relation to the old index?

I know the reindex method

df = pd.DataFrame({'a': [1,2,3,4], 'b': [2,3,4,5]})
df

    a   b
0   1   2
1   2   3
2   3   4
3   4   5

df.reindex([1,2,3,4])

    a   b
1   2.0 3.0
2   3.0 4.0
3   4.0 5.0
4   NaN NaN

... but I just want to modify the row index in order to get

    a   b
1   1   2
2   2   3
3   3   4
4   4   5

Remarks:

  1. I know that I can construct a dataframe with a preassigned index using the DataFrame constructor. I need exactly modify an existent DataFrame.
  2. In this particular case DataFrame.shift can work - but what if I need to assign an arbitrary custom index, not shift the existing one?

Thanks in advance.

like image 809
Name Avatar asked Dec 08 '25 11:12

Name


1 Answers

In chained method calls it might be useful to know the trick that you can call set_index with non-existing labels as long as you wrap the labels you want to set in another list.

>>> df.set_index([[1, 2, 3, 4]])
   a  b
1  1  2
2  2  3
3  3  4
4  4  5
>>>
>>> df.set_index([['w', 'x', 'y', 'z']])
   a  b
w  1  2
x  2  3
y  3  4
z  4  5

I don't really know why this works. Looking at the documentation of set_index my best guess is that this is a special case of creating a multi-index with just one level, like the last example from the documentation

df.set_index([[1, 2, 3, 4], 'year'])

but omitting the second element 'year'.

like image 60
timgeb Avatar answered Dec 09 '25 23:12

timgeb