1

I have more than 100 columns in my pandas data frame.

I want to dynamically parse each column and change the data type of the column.

For example, if the column name has date on it, then change the column to date type, else convert the column to str.

Something like this works for me,

df['Sasd Date'] = df['Sasd Date'].dt.date
df['Sasd'] = df['Sasd'].astype(str)

But I want to do it dynamically for all columns with the above condition, without mentioning the column name.

jpp
  • 159,742
  • 34
  • 281
  • 339

1 Answers1

2

There's no such thing as str series dtype. Strings are typically stored in object dtype series, and this is the default behaviour when reading data into a dataframe from a CSV file / Excel.

Therefore, considering only dates, you can use Boolean indexing:

date_cols = df.columns.str.contains('date', case=False, regex=False)
df[date_cols] = df[date_cols].apply(pd.to_datetime, errors='coerce')

An object dtype series of datetime.date values is not recommended, but it is possible via a custom function:

df[date_cols] = df[date_cols].apply(lambda s: pd.to_datetime(s, errors='coerce').dt.date)
jpp
  • 159,742
  • 34
  • 281
  • 339