Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if column value is in other columns in pandas

Tags:

python

pandas

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.

like image 941
Amyunimus Avatar asked Mar 29 '17 12:03

Amyunimus


People also ask

How do you check if a column value is present in another column in pandas?

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.

How do you check if a value is present in a column in DataFrame?

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 .


2 Answers

You can use drop, isin and any.

  • drop the target column to have a df with your A, B, C columns only
  • check if the values isin the target column
  • and check if any hits are present

That'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
like image 104
pansen Avatar answered Oct 08 '22 19:10

pansen


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
like image 32
MaxU - stop WAR against UA Avatar answered Oct 08 '22 19:10

MaxU - stop WAR against UA