I am needing to format forecast period columns to later merge with another data frame.
Columns of my data frame are:
current_cols = [
'01+11',
'02+10',
'03+09',
'04+08',
'05+07',
'06+06',
'07+05',
'08+04',
'09+03',
'10+02',
'11+01'
]
desired_out = [
'1+11',
'2+10',
'3+9',
'4+8',
'5+7',
'6+6',
'7+5',
'8+4',
'9+3',
'10+2',
'11+1'
]
Originally, I tried to split the list by split('+')
, and use lstrip('0')
for each element in the list. Then recombine elements within tuple with +
in between.
Is there a better approach? I'm having trouble combining elements in tuples back together, with +
in between. Help would be much appreciated.
You can use re
module for the task:
import re
pat = re.compile(r"\b0+")
out = [pat.sub(r"", s) for s in current_cols]
print(out)
Prints:
[
"1+11",
"2+10",
"3+9",
"4+8",
"5+7",
"6+6",
"7+5",
"8+4",
"9+3",
"10+2",
"11+1",
]