Home > Mobile >  Searching Pandas Column for string where each row value contains list
Searching Pandas Column for string where each row value contains list

Time:10-08

I have a pandas series that each values is a list

Component
[R797,R398]
[R803,R93]

I am trying to search within the columns to find a specific component like R797

What I have tried is df[df['component'].str.contains('R797')] but it returns an empty table even though the value is clearly is within the list.

I been trying to iterate through the list but each time it say the list isn't hashable any suggestions would be great

CodePudding user response:

Use pd.explode groupby to find the corresponding indices:

import pandas as pd

# toy data
df = pd.DataFrame(data=[[["R797","R398"]], [["R803","R93"]]], columns=["Component"])

# explode (un-nest) list, then find, and group by index
mask = df.explode("Component").eq("R797").groupby(level=0).any().values

res = df[mask]
print(res)

      Component
0  [R797, R398]

CodePudding user response:

Let's create series:

import pandas as pd
s = pd.Series([['R797','R398'],['R803','R93']])

result:

0    [R797, R398]
1     [R803, R93]

Now apply lambda function to each element (list):

s.apply(lambda s: 'R797' in s)

output:

0     True
1    False
dtype: bool

To find an element:

s[s.apply(lambda s: 'R797' in s)]

result:

0    [R797, R398]
dtype: object
  • Related