Reputation: 21
I have a date column in my pandas dataframe in the form of YYYY-MM-DD, I need to convert this to a integer in the form YYYYMMDD. I have been at it for a long time now and can't find a simple solution.
Upvotes: 2
Views: 5548
Reputation: 153460
If your pandas column is datetime dtype then use datetime access .dt
and strftime
, if you are talking about a datetimeindex then you don't need the .dt
:
df['Date'].dt.strftime('%Y%m%d')
Upvotes: 5
Reputation: 2424
you can try:
df['column_to_change']=df['column_to_change'].str.replace('-','').apply(int)
it will change all strings (or datetime) in your column column_to_change
of you DataFrame df
into format YYYYMMDD with the type integer as you can check with:
df.dtypes
Upvotes: 0