convert date into index on dataframe for default date

Viewed 29

Is it possible to convert a series of dates in a column into a numeric index using pd.to_datetime? After that put it into a variable so I can print the value in a single variable.

default_date = ''
print(f'value of default_date' : {default_date}')

This is the data output enter image description here

For example: 2021-07-01 to 0 2021-07-02 to 1

1 Answers

IIUC use:

df['date'] = pd.to_datetiem(df['date'])
df = df.set_index('date')

Or:

df = df.set_index('date')
df.index = pd.to_datetiem(df.index)
Related