How to get a length of lists in Pandas dataframe

Viewed 2344

I have a dataframe in pandas like this:

                                                             List
2013-12-22 15:25:02  [good morning, good afternoon, good evening]
2009-12-14 14:29:32             [happy new year, happy birthday,]
2013-12-22 15:42:00                      [happy, sad, mad, chill]

how do I get something like this

                                                             List Len
2013-12-22 15:25:02  [good morning, good afternoon, good evening]   3
2009-12-14 14:29:32             [happy new year, happy birthday,]   2
2013-12-22 15:42:00                      [happy, sad, mad, chill]   4

I tried df['List'].str.len(), but it returns the length in terms of how many words are in this list.

5 Answers

I think your solution is nice, if lists in column List:

print (type(df.iat[0, df.columns.get_loc('List')]))
<class 'list'>

df['Len'] = df['List'].str.len()

Solution if not missing values:

df['Len'] = df['List'].apply(len)

If not, first remove possible , in start of end by Series.str.strip and then count number of , with Series.str.count:

print (type(df.iat[0, df.columns.get_loc('List')]))
<class 'str'>

df['Len'] = df['List'].str.strip(' ,[]').str.count(',') + 1
print (df)
                                                             List  Len
2013-12-22 15:25:02  [good morning, good afternoon, good evening]    3
2009-12-14 14:29:32             [happy new year, happy birthday,]    2
2013-12-22 15:42:00                      [happy, sad, mad, chill]    4

If need also convert values to lists:

df['List'] = df['List'].str.strip(' ,[]').str.split(', ')
print (type(df.iat[0, df.columns.get_loc('List')]))
<class 'list'>

df['Len'] = df['List'].str.len()
print (df)
                                                             List  Len
2013-12-22 15:25:02  [good morning, good afternoon, good evening]    3
2009-12-14 14:29:32              [happy new year, happy birthday]    2
2013-12-22 15:42:00                      [happy, sad, mad, chill]    4

You can use this

df['Len']=df['List'].apply(lambda x: len(x))

Use DataFrame.transform

Ex.

df['Len'] = df['List'].transform(len)
print(df)

                                                             List  Len
2013-12-22 15:25:02  [good morning, good afternoon, good evening]    3
2009-12-14 14:29:32              [happy new year, happy birthday]    2
2013-12-22 15:42:00                      [happy, sad, mad, chill]    4

If your List column is str:

df['Len'] = df['List'].map(lambda x: len(x.split(',')))

I do not find any problem with your code. It should work fine. As mentioned by @moys you can also use apply method to create a new column which contains the length of the list column.

df['length']=df['List'].apply(lambda row: len(row))
print(df)
                                                             List  length
2013-12-22 15:25:02  [good morning, good afternoon, good evening]    3
2009-12-14 14:29:32              [happy new year, happy birthday]    2
2013-12-22 15:42:00                      [happy, sad, mad, chill]    4

Let me know if you have any issues regarding this.

Related