I am trying to execute the code inside a function, Which can be used anywhere within the environment but its not working, and giving the Error.
But when i am using the same code without any function it's working fine. i don't understand why is not working when trying to execute within a function
Input Data
Col1
U_a65839_Jan87Apr88
U_b98652_Feb88Apr88_(2).jpg.pdf
V_C56478_mar89Apr89
NaN
Q_d15634_Apr90Apr91
Q_d15634_Apr90Apr91_(3).jpeg.pdf
S_e15336_may91Apr93
NaN
Expected Output:
col2 start_dt end_Dt
Jan87Apr88 Jan1987 Apr1988
Feb88Apr88 Feb1988 Apr1988
mar89Apr89 mar1989 Apr1989
Apr90Apr91 Apr1990 Apr1991
Apr90Apr91 Apr1990 Apr1991
may91Apr93 may1991 Apr1993
Code I have been using
def newmap(x):
try:
df['col2'] = df.col1.str.split("_", expand=True)[2]
# split first part
df['start_dt'] = df.File_Path.str[:5]
# split second part
df['end_dt'] = df.File_Path.str[5:10]
# add 19
df['start_dt'] = df.start_dt.replace({'(\d\d)': r'19\1'}, regex=True)
df['end_dt'] = df.end_dt.replace({'(\d\d)': r'19\1'}, regex=True)
except ValueError:
df['status'] = ValueError
return df
df = newmap(df)
While Running the above code getting the error
AttributeError: 'Series' object has no attribute 'col1'
Please Suggest.