Use a values of list to filter Pandas dataframe

Question:

With python Pandas, I’m trying to filter out the data that contains the specified value in the array, I try to use python in to filter value, but it’s not working, I want to know if there is a way to achieve such a function without looping

import pandas as pd

df = pd.DataFrame({'A' : [1,2,3,4], 'B' : [[1, 2, 3], [2, 3], [3], [1, 2, 3]]})
df = 1 in df['custom_test_type']

    A   B
0   1   [1, 2, 3]
1   2   [2, 3]
2   3   [3]
3   4   [1, 2, 3]

I’m try to filter 1 in row B, so expected output will be:

    A   B
0   1   [1, 2, 3]
3   4   [1, 2, 3]

but the output always be True

due to my limited ability, Any help or explanation is welcome! Thank you.

Asked By: Max Tsai

||

Answers:

You need to use a loop/list comprehension:

out = df[[1 in l for l in df['B']]]

A pandas version would be more verbose and less efficient:

out = df[df['B'].explode().eq(1).groupby(level=0).any()]

Output:

   A          B
0  1  [1, 2, 3]
3  4  [1, 2, 3]
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.