merging the columns in excel file and converting to time format using pandas
Question:
I want to convert the given excel data to a single time data using pandas in python .how can i do that . the format is as follows without header. I also want convert the 12.5 to 12.30 setup
the format should be like this
25 11 2020 12.5 ————-25-11-2020:12:30:00
Answers:
import pandas as pd
from datetime import timedelta
def process_row(row):
date = map(str,map(int,row[:3]))
date = '-'.join(date)
date = pd.to_datetime(date,format='%d-%m-%Y')+timedelta(hours=row[3])
return date
a=[[25,11,2020,12.5],[25,11,2020,13.5],[25,11,2020,14]]
a = pd.DataFrame(a)
print(a)
Output:
0 1 2 3
0 25 11 2020 12.5
1 25 11 2020 13.5
2 25 11 2020 14.0
a['date'] = a.apply(process_row,axis=1)
print(a)
Output:
0 1 2 3 date
0 25 11 2020 12.5 2020-11-25 12:30:00
1 25 11 2020 13.5 2020-11-25 13:30:00
2 25 11 2020 14.0 2020-11-25 14:00:00
I hope, it works for you. I create an custom function and use a datetime library
import pandas as pd
import datetime
df = pd.read_excel('./merging_excel_datetime.xlsx')
def createDate(x):
return datetime.datetime(year=int(x.Year),day=int(x.Day),month=int(x.Month)) + datetime.timedelta(hours=x.Time)
df['Datetime'] = df.apply(lambda x: createDate(x), axis=1)
df
Output:
You need columns names Day, Month, Year
and Hour
for pass to to_datetime
:
L = [[25,11,2020,12.5],
[25,11,2020,13.5],
[25,11,2020,14]]
df = pd.DataFrame(L, columns=['Day','Month','Year','Time'])
df['Date']=pd.to_datetime(df[['Day','Month','Year','Time']].rename(columns={'Time':'Hour'}))
print (df)
Day Month Year Time Date
0 25 11 2020 12.5 2020-11-25 12:30:00
1 25 11 2020 13.5 2020-11-25 13:30:00
2 25 11 2020 14.0 2020-11-25 14:00:00
I want to convert the given excel data to a single time data using pandas in python .how can i do that . the format is as follows without header. I also want convert the 12.5 to 12.30 setup
the format should be like this
25 11 2020 12.5 ————-25-11-2020:12:30:00
import pandas as pd
from datetime import timedelta
def process_row(row):
date = map(str,map(int,row[:3]))
date = '-'.join(date)
date = pd.to_datetime(date,format='%d-%m-%Y')+timedelta(hours=row[3])
return date
a=[[25,11,2020,12.5],[25,11,2020,13.5],[25,11,2020,14]]
a = pd.DataFrame(a)
print(a)
Output:
0 1 2 3
0 25 11 2020 12.5
1 25 11 2020 13.5
2 25 11 2020 14.0
a['date'] = a.apply(process_row,axis=1)
print(a)
Output:
0 1 2 3 date
0 25 11 2020 12.5 2020-11-25 12:30:00
1 25 11 2020 13.5 2020-11-25 13:30:00
2 25 11 2020 14.0 2020-11-25 14:00:00
I hope, it works for you. I create an custom function and use a datetime library
import pandas as pd
import datetime
df = pd.read_excel('./merging_excel_datetime.xlsx')
def createDate(x):
return datetime.datetime(year=int(x.Year),day=int(x.Day),month=int(x.Month)) + datetime.timedelta(hours=x.Time)
df['Datetime'] = df.apply(lambda x: createDate(x), axis=1)
df
Output:
You need columns names Day, Month, Year
and Hour
for pass to to_datetime
:
L = [[25,11,2020,12.5],
[25,11,2020,13.5],
[25,11,2020,14]]
df = pd.DataFrame(L, columns=['Day','Month','Year','Time'])
df['Date']=pd.to_datetime(df[['Day','Month','Year','Time']].rename(columns={'Time':'Hour'}))
print (df)
Day Month Year Time Date
0 25 11 2020 12.5 2020-11-25 12:30:00
1 25 11 2020 13.5 2020-11-25 13:30:00
2 25 11 2020 14.0 2020-11-25 14:00:00