0

I have my dataframe as:

+----+--------------+-------------+-------------+-------------+-----------+----------------+------------------+--------------------+--------------+--------------+------------------+
|    |   Unnamed: 0 | home_odds   | draw_odds   | away_odds   | country   | league         | home_team        | away_team          |   home_score |   away_score | datetime         |
+====+==============+=============+=============+=============+===========+================+==================+====================+==============+==============+==================+
|  0 |       412701 | 167/50      | 329/100     | 63/100      |  Iceland | League Cup     | Kopavogur        | Vikingur Reykjavik |            0 |            1 | 16/02/2016 19:15 |
+----+--------------+-------------+-------------+-------------+-----------+----------------+------------------+--------------------+--------------+--------------+------------------+
|  1 |       412702 | 463/100     | 173/50      | 47/100      |  Iceland | League Cup     | Fram             | Stjarnan           |            0 |            3 | 14/02/2016 21:15 |
+----+--------------+-------------+-------------+-------------+-----------+----------------+------------------+--------------------+--------------+--------------+------------------+
|  2 |       412708 | 51/25       | 263/100     | 109/100     |  Iceland | League Cup     | Keflavik         | Vestmannaeyjar     |            1 |            0 | 14/02/2016 15:00 |
+----+--------------+-------------+-------------+-------------+-----------+----------------+------------------+--------------------+--------------+--------------+------------------+
|  3 |       412710 | 101/100     | 13-May      | 219/100     |  Iceland | League Cup     | Breidablik       | Fylkir             |            1 |            3 | 13/02/2016 12:15 |
+----+--------------+-------------+-------------+-------------+-----------+----------------+------------------+--------------------+--------------+--------------+------------------+
|  4 |       412711 | 51/25       | 287/100     | 51/50       |  Iceland | League Cup     | Throttur         | Leiknir            |            0 |            1 | 12/02/2016 22:00 |
+----+--------------+-------------+-------------+-------------+-----------+----------------+------------------+--------------------+--------------+--------------+------------------+
|  5 |       412712 | 112/25      | 373/100     | 23/50       |  Iceland | League Cup     | Fjolnir          | Hafnarfjordur      |            0 |            4 | 12/02/2016 20:00 |
+----+--------------+-------------+-------------+-------------+-----------+----------------+------------------+--------------------+--------------+--------------+------------------+
|  6 |       412715 | 74/25       | 247/100     | 83/100      |  Hungary | OTP Bank Liga  | Ferencvaros      | MOL Fehervar       |            0 |            1 | 2/10/2011 17:00  |
+----+--------------+-------------+-------------+-------------+-----------+----------------+------------------+--------------------+--------------+--------------+------------------+
|  7 |       412717 | 93/100      | 58/25       | 269/100     |  Hungary | OTP Bank Liga  | Haladas          | Siofok             |            2 |            1 | 1/10/2011 16:00  |
+----+--------------+-------------+-------------+-------------+-----------+----------------+------------------+--------------------+--------------+--------------+------------------+
|  8 |       412718 | 83/100      | 5-Feb       | 59/20       |  Hungary | OTP Bank Liga  | Pecsi MFC        | Kaposvar           |            1 |            1 | 1/10/2011 16:00  |
+----+--------------+-------------+-------------+-------------+-----------+----------------+------------------+--------------------+--------------+--------------+------------------+
|  9 |            9 | 4.66        | 3.74        | 1.59        |  Albania | First Division | Dinamo Tirana    | Beselidhja Lezha   |            2 |            0 | 22/02/2020 14:00 |
+----+--------------+-------------+-------------+-------------+-----------+----------------+------------------+--------------------+--------------+--------------+------------------+
| 10 |           10 | 1.82        | 3           | 4.42        |  Albania | First Division | Beselidhja Lezha | Burreli            |            2 |            1 | 16/02/2020 14:00 |
+----+--------------+-------------+-------------+-------------+-----------+----------------+------------------+--------------------+--------------+--------------+------------------+
| 11 |           11 | 1.41        | 4.2         | 5.85        |  Albania | First Division | Terbuni          | Koplik             |            2 |            1 | 8/02/2020 14:00  |
+----+--------------+-------------+-------------+-------------+-----------+----------------+------------------+--------------------+--------------+--------------+------------------+
| 12 |           15 | 2.51        | 2.98        | 2.64        |  Albania | First Division | Dinamo Tirana    | Egnatia Rrogozhine |            0 |            0 | 26/01/2020 13:00 |
+----+--------------+-------------+-------------+-------------+-----------+----------------+------------------+--------------------+--------------+--------------+------------------+
| 13 |           16 | 2.36        | 3.2         | 2.66        |  Albania | First Division | Elbasani         | Oriku              |            2 |            0 | 25/01/2020 13:00 |
+----+--------------+-------------+-------------+-------------+-----------+----------------+------------------+--------------------+--------------+--------------+------------------+

To filter rows where home_odds, draw_odds and away_odds dont contain "/" I am using the solution posted here

df = df[df.home_odds != "/"]
df = df[df.draw_odds != "/"]
df = df[df.away_odds != "/"]

However its not working.

How can I remove rows containing / ?

2 Answers2

0

Try via str.contains():

m=df[['home_odds', 'draw_odds', 'away_odds']].agg(lambda x:x.str.contains('/'),1).all(1)
#you can also use apply() in place of agg() method

OR

another option via str.count():

m=df[['home_odds', 'draw_odds', 'away_odds']].agg(lambda x:x.str.count('/'),1).ne(0).all(1)

Finally:

df[~m]
#OR
df.loc[~m]

OR

you can do this also in 3 steps:

df = df.loc[~df['home_odds'].str.contains('/')]
df = df.loc[~df['draw_odds'].str.contains('/')]
df = df.loc[~df['away_odds'].str.contains('/')]
Anurag Dabas
  • 23,866
  • 9
  • 21
  • 41
0
if '/' in df.home_odds:
    pass
else:
    do your work

or this can be used

df[~df.Team.str.contains('Fin')]

Shah Vipul
  • 625
  • 7
  • 11