Skip to content Skip to sidebar Skip to footer

Assign New Series To Existing Column By Column Index

I need to replace a date column by formatted strings. I can do it like this: df = pd.DataFrame(pd.date_range('2016-01-01', '2016-01-02'), columns=['date']) df['date'] = df['date'].

Solution 1:

It is a bit hack, but works - select column by position with []:

df = pd.DataFrame([pd.to_datetime('2016-01-01')], columns=['date'])

print (df.columns[0])
date

df[df.columns[0]] = df.iloc[:, 0].dt.strftime('%Y-%m-%d')
print (df)
         date
0  2016-01-01

Post a Comment for "Assign New Series To Existing Column By Column Index"