12

I have a table df

    a   b    c
1   x   y   [x]

2   x   z   [c,d]

3   x   t   [e,f,g]

Just wondering how to select the row using the length of c column

such as

df.loc[len(df.c) >1]

I know this is not right.... what should be the right one?

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Kevin
  • 587
  • 1
  • 7
  • 17

4 Answers4

34

Try this:

df[df.c.map(len)>1]
Tianbo Ji
  • 351
  • 2
  • 4
3

You can using

df.loc[np.array(list(map(len,df.c.values)))>1]
BENY
  • 317,841
  • 20
  • 164
  • 234
1

With apply:

df[df.c.apply(lambda x: len(x) > 1)]
Mac C.
  • 153
  • 10
0

You can also create a fourth column which would contain the length of each list in column "c". Then filter out rows such that entries in the new column is greater than 1.

df["length"] = df["c"].apply(lambda x: len(x) > 1)
df = df.loc[df["length"], :].drop(["length"], axis=1)
dallonsi
  • 1,299
  • 1
  • 8
  • 29