How can I add two numbers to each and every row of a column of a Python dataframe?
I am analyzing a pollution data set. However, the date of each record is in the form 01-01-15, 02-01-15 , which is why I need to format it to 2015 for each row so that I can convert it to datetime . What should I use to do so?
Himanshu Panwar
Software Developer - Data
Use Pandas.
import pandas as pd
r = ['01-01-15','02-01-15','03-01-15'] #creating data
df =pd.DataFrame({'date':r})
#add '20' in year
df.date = df.date.apply(lambda x: x[:6]+'20'+x[6:])
#convert to datetime
df.date = pd.to_datetime(df['date'], format='%m-%d-%Y')
hope this helps. Cheers :)