Home > Back-end >  Read excel dates and convert them to individual strings
Read excel dates and convert them to individual strings

Time:03-04

I recently started using python. I have a series of dates in excel

01-05-2021

02-05-2021

. .

29-05-2021

Now, I want to load this column and convert it into individual strings based on rows. So i can extract the day, month and year separately for each dates

Can someone help me how to do that??

CodePudding user response:

you can do:

df = pd.read_excel("filename.xlsx")

# let's imagine your date column name is "date"

df["day"] = df["date"].apply(lambda elem:elem.split("-")[0])
df["month"] = df["date"].apply(lambda elem:elem.split("-")[1])
df["year"] = df["date"].apply(lambda elem:elem.split("-")[2])

CodePudding user response:

from datetime import datetime

str_time = 01-05-2021 time_convert = datetime.strptime(str_time, '%d-%m-%Y')

print (time_convert, time_convert.day, time_convert.month, time_convert.year)

in your case, make the convert in looping for each data you got from the excel file

  • Related