How to know the number of elements in a column that are stored as an array in Pandas dataframe

32 Views Asked by At

I have a result of a dataframe that looks like this:

Factor      OccurIn
Accelerator [41, 81, 62]
Breaks      [51, 11, 12, 13]
Vision      [58, 31, 92, 67, 68]

How can I copy that frame into a new one having a column summing the number of elements that are in an array like:

Factor      n
Accelerator 3
Breaks      4
Vision      5
1

There are 1 best solutions below

0
On

You can do

df['n']=df['OccurIn'].str.len()

Or

df['n']=df['OccurIn'].map(len)