Date and time
0 2015-04-01 00:00
1 2015-04-01 00:01:00
2 2015-04-01 00:02:00
3 2015-04-01 00:03:00
4 2015-04-01 00:04:00
When I have a data frame like the one above, I would like to combine the date and time as an index such as 2015-04-0100:00:00 and 2015-04-0100:01:00, how should I write it down?
Thank you for your cooperation.
If the date and time column data is of type str, simply combine and then
Why don't you hand it over to Pandas.to_dateime()?
import pandas as pd
importio
data=""Date, Time, Data
2015-04-01,00:00:00,1
2015-04-01,00:01:00,2
2015-04-01,00:02:00,3
2015-04-01,00:03:00,4
2015-04-01,00:04:00,5
"""
df = pd.read_csv(io.StringIO(data))
df['Date'] = pd.to_datetime(df['Date']+'+df['Time'])
df = df.set_index ('Date and Time')
df = df.drop (['date', 'time', axis = 1)
print(df)
[Additional]
For the time being, I will also mention the case where the date and time data are datetime type.
You can do the same by converting it to a string once.
df['date']=pd.to_datetime(df['date'].dt.strftime('%x')+df['time'].dt.strftime('%X')))
© 2024 OneMinuteCode. All rights reserved.