Reputation: 168
I have the following data frame:
>>> print df.head()
>>> print df.dtypes
PRICE
2017-01-05 11:03:58.425000+00:00 340.143
2017-01-05 11:23:37.427000+00:00 216.639
2017-01-05 11:35:10.282000+00:00 201.093
2017-01-05 12:54:10.408000+00:00 201.093
2017-01-05 15:41:38.521000+00:00 201.093
PRICE float64
dtype: object
I want to split this up into two columns, and keep the PRICE
column as type float64
and convert the other column to datetime64[ns]
but I'm having trouble working out how to split a column of type float64!
Desired output:
>>> print df.head()
>>> print df.dtypes
TIMESTAMP PRICE
2017-01-05 11:03:58.425000+00:00 340.143
2017-01-05 11:23:37.427000+00:00 216.639
2017-01-05 11:35:10.282000+00:00 201.093
2017-01-05 12:54:10.408000+00:00 201.093
2017-01-05 15:41:38.521000+00:00 201.093
TIMESTAMP datetime64[ns]
PRICE float64
dtype: object
I can achieve thsi by converting the dataframe into a csv then re-reading it back into another dataframe and then renaming the columns and then converting the columns into their desired types, but it is less pythonic and takes longer to run!
Thanks
Upvotes: 2
Views: 1209
Reputation: 863226
You need reset_index
, because first
column is index
:
df = df.reset_index().rename(columns={'index':'TIMESTAMP'})
print (df)
TIMESTAMP PRICE
0 2017-01-05 11:03:58.425 340.143
1 2017-01-05 11:23:37.427 216.639
2 2017-01-05 11:35:10.282 201.093
3 2017-01-05 12:54:10.408 201.093
4 2017-01-05 15:41:38.521 201.093
Upvotes: 3