I have a df that has 3 unique values within a 'lifecycle interval' column. It is meant to be 4-11 month interval, but the original imported df recognizes it as a datetime.datetime, returning a long and annoying date formatted value. I have included a picture, my attempted code and the expected output:
Attempted code:
df['Lifecycle Interval'].replace(datetime.datetime(2022, 4, 11, 0, 0), '4-11', inplace=True)
Or:
df['Lifecycle Interval'].replace('2022-04-11 00:00:00', '4-11', inplace=True)
Expected output:
df = pd.DataFrame({'Lifecycle Interval':['0-3', '4-11', '12 ']})
df['Lifecycle Interval'].unique()
CodePudding user response:
You can apply a function that converts it to your desired format if it's a datetime, otherwise return the existing value.
import pandas as pd
import datetime
df = pd.DataFrame({'Lifecycle Interval':['0-3',datetime.datetime(2022,4,11,0,0),'12 ']})
df['Lifecycle Interval'] = df['Lifecycle Interval'].apply(lambda x: x.strftime('%#m-%#d') if isinstance(x, datetime.datetime) else x)
Output
Lifecycle Interval
0 0-3
1 4-11
2 12