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:
DataFrame constructor. I need exactly modify an existent DataFrame.DataFrame.shift can work - but what if I need to assign an arbitrary custom index, not shift the existing one?Thanks in advance.
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'.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With