I have the following dataframe in pandas
target A B C
0 cat bridge cat brush
1 brush dog cat shoe
2 bridge cat shoe bridge
How do I test whether df.target
is in any of the columns ['A','B','C', etc.]
, where there are many columns to check?
I have tried merging A,B and C into a string to use df.abcstring.str.contains(df.target)
but this does not work.
Use in operator on a Series to check if a column contains/exists a string value in a pandas DataFrame. df['Courses'] returns a Series object with all values from column Courses , pandas. Series. unique will return unique values of the Series object.
Use the in keyword to check if a value exists in a column of a DataFrame. Use the syntax value in pandas. DataFrame. column_name to determine if value exists in column_name of DataFrame .
You can use drop
, isin
and any
.
drop
the target
column to have a df with your A
, B
, C
columns onlyisin
the target columnany
hits are presentThat's it.
df["exists"] = df.drop("target", 1).isin(df["target"]).any(1)
print(df)
target A B C exists
0 cat bridge cat brush True
1 brush dog cat shoe False
2 bridge cat shoe bridge True
OneHotEncoder approach:
In [165]: x = pd.get_dummies(df.drop('target',1), prefix='', prefix_sep='')
In [166]: x
Out[166]:
bridge cat dog cat shoe bridge brush shoe
0 1 0 0 1 0 0 1 0
1 0 0 1 1 0 0 0 1
2 0 1 0 0 1 1 0 0
In [167]: x[df['target']].eq(1).any(1)
Out[167]:
0 True
1 True
2 True
dtype: bool
Explanation:
In [168]: x[df['target']]
Out[168]:
cat cat brush bridge bridge
0 0 1 1 1 0
1 0 1 0 0 0
2 1 0 0 0 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With