Skip to content
Advertisement

Replacing certain parts of a list in Python

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:

JavaScript

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.

JavaScript

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.

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement