samuelbrody1249
samuelbrody1249

Reputation: 4767

Evaluate based on Truth value of other rows

I have the following data frame:

data=[{
    'directors': 'Ertil Altanaj',
    'director_score': 0.7,
}, {
    'directors': 'Erbil Altanaj',
    'director_score': 0.9,
}, {
    'directors': 'Richard Klemann',
    'director_score': 1.0,
}]
df=pd.DataFrame(data)

To determine whether something has a 'matched score' I can do something like:

df['director_score_match'] = df.director_score >= 0.90

         directors  director_score  director_score_match
0    Ertil Altanaj             0.7                 False
1    Erbil Altanaj             0.9                  True
2  Richard Klemann             1.0                  True

I would like to determine if an other row (i.e., any other row excluding itself) has a True value for that field. For example, the result should be:

         directors  director_score  director_score_match      director_score_other_match
0    Ertil Altanaj             0.7                 False      True
1    Erbil Altanaj             0.9                 True       True
2  Richard Klemann             1.0                 True       True

How would I do that calculation in pandas?

Upvotes: 0

Views: 22

Answers (2)

Georgina Skibinski
Georgina Skibinski

Reputation: 13387

This should do the trick:

df['director_score_other_match']=df['director_score_match'].astype(int).lt(df['director_score_match'].astype(int).sum())

Some explanation:

You convert bool to int (1/0). Then the only case when field should be False is when either all are False or this single one is True - hence you make sure, that the current 1/0 is less than the total sum.

Upvotes: 1

Shadab Hussain
Shadab Hussain

Reputation: 814

Try this code:

df['director_score_other_match'] = df['director_score_match'].apply(lambda x: not(x))

Upvotes: 0

Related Questions