1

Currently i have a dataframe looking like this:

import pandas as pd
import numpy as np

col1 = [2, 3, 3, 2]
col2 = [[1,3,2], 4, 2, [1,2]]

df = pd.DataFrame({"A": col1, "B": col2})
   A   B
0  2   1,3,2
1  3   4
2  3   2
3  2   1,2

if i run df.loc[df['B']== 4] it runs fine. However if i try to run df.loc[df['B']== [1,3,2]] it gives me an error: 'Lengths must match to compare'. Is there a way i can compare a list within my pandas series? The desired outcome is ofcourse is the first row of index 0 as output.

kGame
  • 21
  • 3

1 Answers1

2

To compare with list you have to use map/apply:

df[df['B'].map(lambda x: x == [1, 3, 2])]

   A          B
0  2  [1, 3, 2]
Shubham Sharma
  • 68,127
  • 6
  • 24
  • 53