Gediminas Sadaunykas
Gediminas Sadaunykas

Reputation: 439

Check for values in data frame columns, return one.

Probably a quick and easy task for most of the people here. Though, for me starter in python in gave some problems. Lets say we have:

DATA.

g=pd.DataFrame({'AGGRESSION':[0,1,0,0,1], 'DECEPTIVENESS':[0,1,0,0,0]},index=    [1,2,3,4,5])

QUESTION AND DESIRED RESULT. I would like to iterate through rows check for rows where aggresiveness and deceptivness both equal to 1, leave 1 only for deceptivness in the relevant row.

g['AGRESSION']=[0,0,0,0,1]
g['DECEPTIVENESS']=[0,1,0,0,0]

Upvotes: 0

Views: 44

Answers (1)

akuiper
akuiper

Reputation: 214957

You can use Series.where:

g['AGGRESSION'] = g['AGGRESSION'].where(~((g['AGGRESSION'] == 1) & (g['DECEPTIVENESS'] == 1)), 0)

enter image description here

or Simply:

g['AGGRESSION'][(g['AGGRESSION'] == 1) & (g['DECEPTIVENESS'] == 1)] = 0

Upvotes: 2

Related Questions