i currently have a dataset
date_checkout_y
18000809 5
18001017 6
18001226 8
18001229 8
18010216 10
i want to split date_checkout_y to create a datetime
tried
df['date_checkout_y'] = df.date_checkout_y.str.split(n=1,expand=True)
but getting an error
CodePudding user response:
use from apply method (if your col is string else convert it to string)
from datetime import datetime
def f(x):
return datetime(year=x[:4], month=x[4:6], day=x[6:8]
df['new'] = df['date_checkout_y'].apply(f)
CodePudding user response:
Would the following code snippet provide you with the results you desire?
import pandas as pd
df = pd.DataFrame([['18000809 5'], ['18001017 6'], ['18001226 8'], ['18001229 8'], ['18001229 8']], columns=['date_checkout_y'])
print(df)
df2 = df.date_checkout_y.str.split(n=1,expand=True)
df2.columns = ['Date', 'Value']
print(df2)
When I ran this "proof of principle" program with data that mimicked yours, this was the result I got on my terminal.
date_checkout_y
0 18000809 5
1 18001017 6
2 18001226 8
3 18001229 8
4 18001229 8
Date Value
0 18000809 5
1 18001017 6
2 18001226 8
3 18001229 8
4 18001229 8
Hope that helps.
Regards.