I wish to replace the blank space w a '.' and then shorten the length of the numerical values (ex 2022) by removing the first 2 characters.
Data
df =
ID Date
AA Q1 2022
BB Q4 2024
CC Q2 2025
CC Q3 2025
CC Q1 2021
DD Q2 2021
Desired
ID Date
AA Q1.22
BB Q4.24
CC Q2.25
CC Q3.25
CC Q1.21
DD Q2.21
Doing
1.replace blank with '.'
df.Date.fillna(value=., inplace=True)
2.Remove first 2 digits from year
df["Date"].str[2:]
3. possibly concat?
Any suggestion is appreciated.
CodePudding user response:
Use Series.replace
with a regex pattern (regex=True
):
df['Date'] = df['Date'].replace(r'\s\d{2}','.',regex=True)
print(df)
ID Date
0 AA Q1.22
1 BB Q4.24
2 CC Q2.25
3 CC Q3.25
4 CC Q1.21
5 DD Q2.21
Explanation regex pattern \s\d{2}
:
\s
matches any whitespace character\d
matches a digit{2}
matches the previous token (i.e. the digit) exactly 2 times