I have a list of columns that represents months for different metrics. It was imported from a CSV. There are two issues: (1) The months start with ’22-‘ and (2) some months end with .1 , .2 etc.
Here is the list:
months = ['22-Apr', '22-May', '22-Apr.1', '22-Apr.2']
Tried using .replace but I can’t get it to remove only certain parts of the list
Advertisement
Answer
Among others, you could split by '-' and get the second part, then by '.' and get the first part. The latter will work even if there is no '.' in the string.
>>> months = ['22-Apr', '22-May', '22-Apr.1', '22-Apr.2']
>>> [s.split("-")[1].split(".")[0] for s in months]
['Apr', 'May', 'Apr', 'Apr']
The [1] means that this will work only if there is always a year-part present, but it does not matter if that has 2 or 4 digits.