0

I'm converting a datetime column (referred to as DATE) in my Pandas dataframe df to a string of the form 'Ymd' (e.g. '20191201' for December 1st 2019). My current way of doing that is:

import datetime as dt

df['DATE'] = df['DATE'].apply(lambda x: dt.datetime.strftime(x, '%Y%m%d'))

But this is surprisingly inefficient and slow when run on large dataframes with millions of rows. Is there a more efficient alternative I am not seeing? That would be extremely helpful. Thanks.

Notna
  • 461
  • 1
  • 8
  • 18

1 Answers1

1

In pandas you do not need apply

df['Date']=df['DATE'].dt.strftime('%Y%m%d')
BENY
  • 296,997
  • 19
  • 147
  • 204