Based on a DataFrame that contains dates
import pandas as pd
df = pd.DataFrame({'month':['2','5','8'],'year':['2001',' 89','1999']})
print(df)
month year
0 2 2001
1 5 89
2 8 1999
I want to prefix all year instances consisting of only 2 digits by 19, such that the resulting DataFrame is
month year
0 2 2001
1 5 1989
2 8 1999
I tried
pattern = r'[^\d]*\d{2}[^\d]*'
replacement = lambda m: '19'+m
df.year = df.year.str.replace(pattern,replacement)
print(df)
month year
0 2 NaN
1 5 NaN
2 8 NaN
Which does not work. What is the problem?
df['year'] = df['year'].str.strip().apply(lambda x: '19' + x if len(x) == 2 else x)?19in all cases?