Skip to content
Advertisement

Pandas (How to Fix): List is actually string and the value of length is misleading

I have a dataframe with a list of years in the first column. A second column shows the number of years listed in each row.

JavaScript

Which made me think that the contents of each cell is a pure string. And it seems that way when I checked the type:

JavaScript

When I convert the column to list using to_list(), it shows:

JavaScript
JavaScript

How do I convert it so that the Count_of_Years shows correct values?

Advertisement

Answer

If the values in Years column are already strings then I would suggest to use the str.count method with a regex pattern to find the number of matching occurrences:

JavaScript

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