lolikianoaholting
lolikianoaholting

Reputation: 31

Calculating Length of List of List in Pandas

I have a problem that is extension to this and this. Suppose I have dataframe like this:

    A   B
0   1   [["Thing_1"]]
1   2   [["Thing_1"], ["Thing_2"]]
2   3   [["Thing_1", "Thing_2"], ["Thing_2"]]
3   4   [["Thing_1"], ["Thing_1", "Thing_2"]]
4   5   [["Thing_1", "Thing_2"], ["Thing_1", "Thing_2"]]

Instead of getting like this:

    A   B                                                 C(result of length in column B)
0   1   [["Thing_1"]]                                     1
1   2   [["Thing_1"], ["Thing_2"]]                        2
2   3   [["Thing_1", "Thing_2"], ["Thing_2"]]             2
3   4   [["Thing_1"], ["Thing_1", "Thing_2"]]             2
4   5   [["Thing_1", "Thing_2"], ["Thing_1", "Thing_2"]]  2

the column C becomes the length of the characters in the lists of lists rather than the outer length of lists of lists using these functions: df['B'].str.len() and df['B'].apply(len). How can I get the result right?

Upvotes: 1

Views: 200

Answers (1)

MrNobody33
MrNobody33

Reputation: 6483

I think maybe your column B is actually of type string, since it's giving you a length of the characters, so try first with ast.literal_eval to change the column from string type to lists, and then use df['B'].apply(len) or df['B'].str.len()

import ast
df['B']=df['B'].apply(ast.literal_eval)
df['C']=df['B'].apply(len)

Or if you only need the length without changing the type of column B, try with a single apply

df['C']=df['B'].apply(lambda x:ast.literal_eval(x)).str.len()

Output:

df
   A                                                 B  C
0  1                                     [["Thing_1"]]  1
1  2                        [["Thing_1"], ["Thing_2"]]  2
2  3             [["Thing_1", "Thing_2"], ["Thing_2"]]  2
3  4             [["Thing_1"], ["Thing_1", "Thing_2"]]  2
4  5  [["Thing_1", "Thing_2"], ["Thing_1", "Thing_2"]]  2

Upvotes: 1

Related Questions