Reputation: 925
I am trying to change a certain index value of a dataframe. The dataframe looks like this:
start stop nested_in
0 2015-11-10 05:42:00+00:00 2015-11-10 07:22:00+00:00 -1.0
0 2015-11-10 05:42:00+00:00 2015-11-10 06:09:00+00:00 0.0
0 2015-11-10 06:21:00+00:00 2015-11-10 06:37:00+00:00 0.0
0 2015-11-10 06:42:00+00:00 2015-11-10 06:58:00+00:00 0.0
0 2015-11-10 17:00:00+00:00 2015-11-10 21:55:00+00:00 -1.0
0 2015-11-10 17:00:00+00:00 2015-11-10 17:45:00+00:00 4.0
0 2015-11-10 17:45:00+00:00 2015-11-10 18:01:00+00:00 4.0
With 0's being the index.
I want to do something like this:
for i in range(0, df.size):
df.index[i] = i
But this gives me following error
TypeError: Index does not support mutable operations
All I am able to do is this:
df.index = [i1, i2,... , i(df.size-1)]
So for this example:
df.index = [0,1,2,3,4,5,6]
The output I want is this:
start stop nested_in
0 2015-11-10 05:42:00+00:00 2015-11-10 07:22:00+00:00 -1.0
1 2015-11-10 05:42:00+00:00 2015-11-10 06:09:00+00:00 0.0
2 2015-11-10 06:21:00+00:00 2015-11-10 06:37:00+00:00 0.0
3 2015-11-10 06:42:00+00:00 2015-11-10 06:58:00+00:00 0.0
4 2015-11-10 17:00:00+00:00 2015-11-10 21:55:00+00:00 -1.0
5 2015-11-10 17:00:00+00:00 2015-11-10 17:45:00+00:00 4.0
6 2015-11-10 17:45:00+00:00 2015-11-10 18:01:00+00:00 4.0
I did some research but couldn't find a straight-up easy solution.
Upvotes: 3
Views: 1389
Reputation: 9081
Try this -
indices = range(df.shape[0]) # this can be anything as long as the length is same as that of the number of rows in the dataframe
df['indices'] = indices
df = df.set_index('indices')
print df.head()
Upvotes: 0