Having a data frame as below:
Day | Month and year |
---|---|
13 | septiembre /98 |
15 | August/98 |
24 | Novem /98 |
Is it possible that i can merge day with month and year and create a new column.
Day | Month and year | Date |
---|---|---|
13 | septiembre /98 | 13-09-98 |
15 | August/98 | 15-08-98 |
24 | Nov /98 | 24-11-98 |
CodePudding user response:
I was able to create a panda Series that converts the data you provided from a list to a new list, and then obtains a panda Series from the new, correctly formatted list. I'm not sure if that's what you wanted, but anyway I hope this can be of some help:
import pandas as pd
day = [13, 15, 24]
monthyear = ['Semptember/98', 'August/98', 'November/98']
daymonthyear = zip(day, monthyear)
daymonthyear_new = []
for i in daymonthyear:
teste = [i[0],i[1].split("/")]
string = str(teste[0]) "-" str(teste[1][0]) "-"
str(teste[1][1])
print('string= ', string)
daymonthyear_new.append(string)
print('daymonthyear_new= ', daymonthyear_new)
import datetime
dates = pd.Series(daymonthyear_new)
dates
CodePudding user response:
You could perform string slicing and concatenation provided that your dataset comes in a predictable and standard format. Cast this new string to datetime using the pd.to_datetime
method.
For example, this would work for your example:
import pandas as pd
df = pd.DataFrame([[13, 'Septiembre /98'], [15, 'August/98'], [24, 'Novem /98']], columns=["Day", "Month and year"])
df['Date'] = pd.to_datetime(
df['Day'].astype('str')
' - '
df['Month and year'].str.slice(0, 3)
' - '
df['Month and year'].str.slice(-2)
)
print(df)
Day Month and year Date
0 13 Septiembre /98 1998-09-13
1 15 August/98 1998-08-15
2 24 Novem /98 1998-11-24