syedmfk
syedmfk

Reputation: 111

how to check if a list value exist in a column value which is also a list

I have the following Dataframe:

    Name
0   [Scrap]
1   [Machinery, Scrap]
2   [RMG]
3   [Leather]
4   [Machinery]
5   [RMG, Optical Frame, Machinery]

I want to check if "Scrap" or "Machinery" exist in Name column. like below:

Name                                  Type
0   [Scrap]                           True
1   [Machinery, Scrap]                True
2   [RMG]                             False
3   [Leather]                         False
4   [Machinery]                       True
5   [RMG, Optical Frame, Machinery]   True

I have tried the following code.

df['Type'] = df.apply(lambda x: ["Scrap", "Iron","Machinery"] in x["Name"], axis=1)

However, i get the following result:

Name                                Type
0   [Scrap]                         False
1   [Machinery, Scrap]              False
2   [RMG]                           False
3   [Leather]                       False
4   [Machinery]                     False
5   [RMG, Optical Frame, Machinery] False

Upvotes: 0

Views: 42

Answers (3)

Korhan
Korhan

Reputation: 361

You can also use set intersection to see whether there exists a common element.

df['Type'] = df.apply(lambda x: bool( set(["Scrap", "Iron","Machinery"]) & set(x["Name"])  ), axis=1)

Upvotes: 0

PeeteKeesel
PeeteKeesel

Reputation: 772

You are checking if the whole list can be found, which is why you are getting no True. Try

df['Type'] = df.apply(lambda elem: ("Scrap" in elem["Name"]) or ("Machinery" in elem["Name"]), axis=1)

Upvotes: 0

Rakesh
Rakesh

Reputation: 82805

This is one approach using .apply.

Ex:

to_check = ("Scrap", "Iron","Machinery") 
df = pd.DataFrame({"Name": [["Scrap"], ["Machinery", "Scrap"], ["RMG"], ["Leather"], ["Machinery"], ["RMG", "Optical Frame", "Machinery"]]})
df['Type'] = df["Name"].apply(lambda x: any(i in x for i in to_check))

print(df)

Output:

                              Name   Type
0                          [Scrap]   True
1               [Machinery, Scrap]   True
2                            [RMG]  False
3                        [Leather]  False
4                      [Machinery]   True
5  [RMG, Optical Frame, Machinery]   True

Upvotes: 1

Related Questions