0

I have a dataframe that has an array as one of its values

   Alg    iMap_x
0  Max()  [12,34]
1  Min()  [43,11,84,33]
2  Sum()  [93,15,3,99,37]

I want to filter my dataframe based on iMap_x value

I have i = [43,11,84,33]

when i try this code it does not work

df[df["iMap_x"] == i]["Alg"]

this gives error

ValueError: ('Lengths must match to compare', (4,), (2,))

I also tried

df[df["iMap_x"].isin(i)]["Alg"]

but gave no values (empty dataframe)

I tried

df["iMap_x"].isin(i)

but gave all false

0    False
1    False
2    False
3    False

any idea how to do that?

1
  • One possibility from the dupe: df[df.iMap_x.map(tuple).isin([tuple(i)])] Commented May 30, 2022 at 1:30

1 Answer 1

0

Here's a way to do what your question asks:

df2 = df['iMap_x'].apply(lambda x: x == i)

Full test code:

import pandas as pd
df = pd.DataFrame({'Alg':['Max()','Min()','Sum()'], 'iMap_x':[[12,34],[43,11,84,33],[93,15,3,99,37]]})
print('df:',df,sep='\n')

i = [43,11,84,33]
df2 = df['iMap_x'].apply(lambda x: x == i)
print('df2:',df2,sep='\n')

Input:

df:
     Alg               iMap_x
0  Max()             [12, 34]
1  Min()     [43, 11, 84, 33]
2  Sum()  [93, 15, 3, 99, 37]

Output:

df2:
0    False
1     True
2    False

If you want to use this criteria to filter rows then select the Alg column, you can do this:

df3 = df[df['iMap_x'].apply(lambda x: x == i)]['Alg']
print(df3)

Output:

1    Min()
Name: Alg, dtype: object
Sign up to request clarification or add additional context in comments.

1 Comment

@asmgx Do you need additional help with your question?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.