0

I have the following dataframe -

ID | Column1 |
0  |  []     | 
1  |  [1,2]  | 
2  |  []     |

I want a column which gives the length of the list in column1. Result should look like -

ID | Column1 | Column2 |
0  |  []     |   0     |
1  |  [1,2]  |   2     |
2  |  []     |   0     |

I tried using lambda function but it is not giving the number of rows in the data frame as every entry in column 2 -

df1 = df.assign(Column2 = lambda x: (len(x['Column1'])))

Can someone please help me out here?

tester559
  • 31
  • 4

1 Answers1

0

I'd iterate through each element in Column1, get its length, save it in a list and then assign it to a new Column2. This would be summarized with:

df['Column2'] = [len(x) for x in df['Column1']]
akis
  • 146
  • 1
  • 9