BTurkeli
BTurkeli

Reputation: 103

Dataframe of lists: get the values having length more than 1

Let's say I have a dataframe like this:

df = pd.DataFrame(data=np.array([[1],[1,2],[1,2,3],[2]]), columns=['col'])

I need to reach finally below sub-list:

    col
0   [1, 2]
1   [1, 2, 3] 

Which means I need to "get the rows having length > 1" or "drop rows having length <= 1". How can I do this?

Upvotes: 0

Views: 2232

Answers (1)

Djaouad
Djaouad

Reputation: 22776

You can filter using .loc and set a condition on the length of the 'col' column, then, use reset_index on the resulting dataframe to ignore the index of the old dataframe:

print(df.loc[df.col.str.len() > 1].reset_index(drop=True))

Output:

         col
0     [1, 2]
1  [1, 2, 3]

Upvotes: 2

Related Questions