0

enter image description here

I tried below mention code but unable to execute successfully, it happening due to other type entry in the same column

df['column1']=df['column1'].dt.strftime("%d %b %Y")

getting AttributeError: Can only use .dt accessor with datetimelike values

sample of dataframe:(picture attached)

Himanshu
  • 666
  • 1
  • 8
  • 18
MD SAJID
  • 106
  • 1
  • 8
  • Does this answer your question? [How to change the datetime format in pandas](https://stackoverflow.com/questions/38067704/how-to-change-the-datetime-format-in-pandas) – Tayyab Mar 05 '20 at 06:48
  • no... actually i have to ignore the other format entry available in the column – MD SAJID Mar 05 '20 at 06:49
  • added of data frame sample, hope it will give you more clear idea – MD SAJID Mar 05 '20 at 06:53
  • Better than a table would be minimal code sample which creates this table and fills with two values - one having right format and second not. It will be easier for us to execute it, see error and propose fix. – Łukasz Ślusarczyk Mar 05 '20 at 07:07
  • @ŁukaszŚlusarczyk can you please add some example – MD SAJID Mar 05 '20 at 07:11

1 Answers1

1

Try this:

from datetime import datetime

def converter(x):
    try:
        return(datetime.strptime(x, "%Y-%m-%d %H:%M:%S").strftime("%b %d %Y"))
    except:
        return x
df['column1'] = df['column1'].apply(converter)
Himanshu
  • 666
  • 1
  • 8
  • 18
  • thanks, but I try to write the code in which we just convert the right format else ignore the other cases, in the same column instead of creating other columns – MD SAJID Mar 05 '20 at 07:17