So basically what I am trying to do is to format date column. Dates are given as : 24th Mar, 5th Jul and so on. I wrote a function to split these and make it like 24/03 and 05/07. But the problem is that for rows 0 to 8 in my pandas data frame it is for 2021 and rest of the rows is for 2020. So basically with the current code I can make 24th Mar to 24/03 but I want it to be 24/03/2021 if row number is between 0 to 8 and 24/03/2021 if row number is after 8.
import operator
def dateConversion(date):
day =''
month = ''
val_month = 0
if operator.contains(date, "th"):
day, month = date.split("th")
if operator.contains(date, "rd"):
day, month = date.split("rd")
if operator.contains(date, "nd"):
day, month = date.split("nd")
if operator.contains(date, "st"):
day, month = date.split("st")
day = day.strip()
if(int(day) < 10):
day = str(day)
day = '0' + day
month = month.strip()
months = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] #list items based on data
if month in months:
val_month = months.index(month) + 1 #getting month value
if(val_month < 10):
val_month = str(val_month)
val_month = '0' + val_month
day = day + '/' + str(val_month) #+ '/' + year
return day
And I have used below code to apply to column :
df_ipo['Listed Date_'] = df_ipo['Listed Date'].apply(lambda x: dateConversion(x))
How can I pass the row number as well with this apply function in dateConversion so that I can set year accordingly.