I want to filter a DataFrame in this way, base on dictionary values.
dict = {
101 : 2500,
102 : 2700,
103 : 2000,
}
And a DataFrame in this way:
| idx | match_id | type_name | second |
|---|---|---|---|
| 1 | 101 | Pass | 2400 |
| 2 | 101 | Shot | 2450 |
| 3 | 101 | Match_end | 2500 |
| 4 | 102 | Pass | 2700 |
| 5 | 102 | Match_end | 2600 |
| 6 | 103 | Match_end | 2000 |
I want a line of code that returns me the match_id values where type_name==Match_end and second is equal to the value inside the dictionary where the key has the same value of the match_id.
In this case I'd like to returns this list : [101,103]
Because, inside the DataFrame, the 3 and 6 rows respects the conditions (5 no because its second value is not the same of dict.get(102)).
I tried to use this code but without success because with loc I'm not able to use the relative index:
list = list(
df.loc[
(df["type_name"]=="Match_end")
& (df["second"] == dict.get(df["match_id"]))
]["match_id"].values
)
I need something inside the second condition which help me to use the dictionary based on match_id values of each row.
Does someone has some suggests to do this thing (with or without loc)?
NB I KNOW HOW TO DO THIS USING A FOR CICLE OVER "match_id" BUT I'M LOOKING FOR A METHOD TO DO THIS WITHOUT USE A FOR LOOP.
Thanks