2

I am getting the following series. I want to calculate the length of list for each country.

Scotland                [1074957, 1074964, 1074968, 1074970, 287855, 3...
South Africa            [1020029, 1031431, 1031433, 1031435, 222678, 2...
Sri Lanka               [1001349, 1001351, 1001353, 1083449, 1083450, ...
United Arab Emirates    [1072206, 1072207, 1072208, 1074962, 1074965, ...
West Indies             [1041615, 1041617, 1050217, 1050219, 1050221, ...
Zimbabwe                [1007655, 1007657, 1007659, 287856, 287858, 41...
Name: Id, dtype: object

such that the resulting series OR Dataframe will be

Scotland              35
South Africa          57
Sri Lanka             12
United Arab Emirates  31
West Indies           74
Zimbabwe               9

In Pandas how can we do this in a Pythonic way?

muazfaiz
  • 4,611
  • 14
  • 50
  • 88

1 Answers1

5

Use str.len() only:

a.str.len()

And for columns of DataFrame:

df['col'].str.len()

But if no NaNs values apply(len) working more efficient:

a.apply(len)

df['col'].apply(len)

List comprehension solutions:

pd.Series([len(x) for x in a], index=a.index)
pd.Series([len(x) for x in df['col']], index=df.index)
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • 1
    Glad I found this, this is really un-intuitive - especially if you've just applied `literal_eval` to the lists in your series stored in a file to import to the dataframe. the last place you'd look for list methods is in `str` methods – lys Feb 20 '21 at 08:50