How do I modify the isin python code?

256    Asked by ClaudineTippins in Data Science , Asked on Feb 15, 2023

Given a list of strings L1 L1 = ['a', 'b', 'c'], I need to extract the rows which contain the values given in list L1. I used the isin function: df[df['column1'].isin(L1)]


The data contains the following values in a column 1:


'a'
'c'
'a, d'
'brp'

The data contains the following values in a column 2:


['a']
['c']
['a', 'd']
['brp']

The output I need should print all the rows because the string 'a' is present in L1, but, the output returns only 3 rows: rows 1, 2 (that is the rows containing strings 'a', 'c')


How do I modify the code so that it returns the 3rd row as well?

Answered by David Edmunds

You can use the str.contains method for is in python code using a regex pattern:


import pandas as pd
L1 = ["a", "b", "c"]
df = pd.DataFrame({
    "column1": ["a", "c", "a, d", "brp"]
})
# use the '|' character to check if the strings contains any of the characters in L1
df[df["column1"].str.contains("|".join(L1))]

Your Answer

Interviews

Parent Categories