Home > Net >  Skipping formatting certain values in python dataframe
Skipping formatting certain values in python dataframe

Time:11-26

I have a python dataframe. My column Date-Time contains dates and times. Some are formatted as dates and some were typed in as strings. Using python I want to skip cells that are of type str and apply this function to the rest:

df['Date-Time'] = df['Date-Time'].apply(lambda x: x.strftime('%d/%m/%Y %H:%M:%S'))

is this possible?

CodePudding user response:

Try this way:

df['Date-Time'] = df['Date-Time'].apply(lambda x: x.strftime('%d/%m/%Y %H:%M:%S') if isinstance(x, datetime.date) else x)

This will format all date values and keep the others as they are

  • Related