-2

I am in need of some guidance and/or help! I have a dataframe that has a unique observation for each row. There is a variable that has THE APPEARANCE of nested lists as the observation but is actually a string.

For example:

 Var1
 [X, Y, [Z, A, B]]
 [A, [R,S,T]]
 [B]

What I need is to append a variable/series that has the count of the number of items within that list. So for example:

 Var2:
 5
 4
 1

The values are strings and I need a count not a sum. That the lists are nested is irrelevant, each item separated by a comma is considered 1 count.

Thanks so much! I don't know if I'm searching the wrong terms, but any time I do a length statement (whether I use for loops or not), I get the length of characters, not the length of items.

ETA: Realized that it's actually a string for the observations.

  • isn't this a duplicate of: https://stackoverflow.com/questions/27761463/how-can-i-get-the-total-number-of-elements-in-my-arbitrarily-nested-list-of-list – anishtain4 Apr 24 '18 at 19:08
  • 1
    Possible duplicate of [How can I get the total number of elements in my arbitrarily nested list of lists?](https://stackoverflow.com/questions/27761463/how-can-i-get-the-total-number-of-elements-in-my-arbitrarily-nested-list-of-list) – TwistedSim Apr 24 '18 at 19:17
  • It's possibly a duplicate -- I'm new to using Python for data analysis/data science so I'm probably not searching for the right terms. – Snarl Marx Apr 24 '18 at 19:32
  • I can't seem to get a list of the values to append to the dataframe. This dataset has about 1,000 observations so I need a count for each observation. – Snarl Marx Apr 24 '18 at 19:34

1 Answers1

0

You can using np.hstack

list(map(lambda x :len(np.hstack(x)),df.Var1))
Out[10]: [5, 4, 1]


df['Var2']=list(map(lambda x :len(np.hstack(x)),df.Var1))
df
Out[12]: 
                Var1  Var2
0  [X, Y, [Z, A, B]]     5
1     [A, [R, S, T]]     4
2                [B]     1
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Hmmm, so that didn't work. I get the length of all the characters. The variable includes a list of toppings, so it's more set up like: [Fresh Tomato Salsa, [Rice, Black Beans]] When I run it on that, I just get the lengths of characters instead of a 3. – Snarl Marx Apr 24 '18 at 19:30