This question is related to the thread How to change the datetime format in pandas
The solutions presented there work fine for one date, but what if you have different dates in the same dataframe?
For instance, my dates in datetime are
2016-01-26
2016-01-27
2016-01-28
2016-01-29
Code as in the other thread where the same date is used twice:
import pandas as pd
df = pd.DataFrame({'DOB': {0: '26/1/2016', 1: '26/1/2016'}})
print (df)
DOB
0 26/1/2016
1 26/1/2016
df['DOB'] = pd.to_datetime(df.DOB)
print (df)
DOB
0 2016-01-26
1 2016-01-26
df['DOB1'] = df['DOB'].dt.strftime('%m/%d/%Y')
print (df)
DOB DOB1
0 2016-01-26 01/26/2016
1 2016-01-26 01/26/2016
I also found this code on (I think it was) datetime to string with series in python pandas
df['DOB1'] = df['DOB'].apply(lambda x:x.strftime('%m/%d/%Y'))
And this one:
df['DOB1'] = df['DOB'].map(lambda x:x.strftime('%m/%d/%Y'))
For instance in my dataframe DOB I have the following dates and would like to have them in a different format.
My dates in Datetime format:
0 2016-01-26
1 2016-01-27
2 2016-01-28
3 2016-01-29
Desired output:
01/26/2016
01/27/2016
01/28/2016
01/29/2016
I've been using the same codes as above but when I run them (no matter which of the codes) ALL the dates get turned into
01/26/2016
01/26/2016
01/26/2016
01/26/2016
It seems like pandas takes the first value and applies it to the rest of the dataframe. How can I make pandas read and convert each of the dates differently?
Thank you!