Deleting DataFrame row in Pandas based on column value

前端 未结 10 2253
悲哀的现实
悲哀的现实 2020-11-22 04:08

I have the following DataFrame:

             daysago  line_race rating        rw    wrating
 line_date                                                 
 2007         


        
相关标签:
10条回答
  • 2020-11-22 04:28

    Another way of doing it. May not be the most efficient way as the code looks a bit more complex than the code mentioned in other answers, but still alternate way of doing the same thing.

      df = df.drop(df[df['line_race']==0].index)
    
    0 讨论(0)
  • 2020-11-22 04:32

    But for any future bypassers you could mention that df = df[df.line_race != 0] doesn't do anything when trying to filter for None/missing values.

    Does work:

    df = df[df.line_race != 0]
    

    Doesn't do anything:

    df = df[df.line_race != None]
    

    Does work:

    df = df[df.line_race.notnull()]
    
    0 讨论(0)
  • 2020-11-22 04:33

    The best way to do this is with boolean masking:

    In [56]: df
    Out[56]:
         line_date  daysago  line_race  rating    raw  wrating
    0   2007-03-31       62         11      56  1.000   56.000
    1   2007-03-10       83         11      67  1.000   67.000
    2   2007-02-10      111          9      66  1.000   66.000
    3   2007-01-13      139         10      83  0.881   73.096
    4   2006-12-23      160         10      88  0.793   69.787
    5   2006-11-09      204          9      52  0.637   33.106
    6   2006-10-22      222          8      66  0.582   38.408
    7   2006-09-29      245          9      70  0.519   36.318
    8   2006-09-16      258         11      68  0.486   33.063
    9   2006-08-30      275          8      72  0.447   32.160
    10  2006-02-11      475          5      65  0.165   10.698
    11  2006-01-13      504          0      70  0.142    9.969
    12  2006-01-02      515          0      64  0.135    8.627
    13  2005-12-06      542          0      70  0.118    8.246
    14  2005-11-29      549          0      70  0.114    7.963
    15  2005-11-22      556          0      -1  0.110   -0.110
    16  2005-11-01      577          0      -1  0.099   -0.099
    17  2005-10-20      589          0      -1  0.093   -0.093
    18  2005-09-27      612          0      -1  0.083   -0.083
    19  2005-09-07      632          0      -1  0.075   -0.075
    20  2005-06-12      719          0      69  0.049    3.360
    21  2005-05-29      733          0      -1  0.045   -0.045
    22  2005-05-02      760          0      -1  0.040   -0.040
    23  2005-04-02      790          0      -1  0.034   -0.034
    24  2005-03-13      810          0      -1  0.031   -0.031
    25  2004-11-09      934          0      -1  0.017   -0.017
    
    In [57]: df[df.line_race != 0]
    Out[57]:
         line_date  daysago  line_race  rating    raw  wrating
    0   2007-03-31       62         11      56  1.000   56.000
    1   2007-03-10       83         11      67  1.000   67.000
    2   2007-02-10      111          9      66  1.000   66.000
    3   2007-01-13      139         10      83  0.881   73.096
    4   2006-12-23      160         10      88  0.793   69.787
    5   2006-11-09      204          9      52  0.637   33.106
    6   2006-10-22      222          8      66  0.582   38.408
    7   2006-09-29      245          9      70  0.519   36.318
    8   2006-09-16      258         11      68  0.486   33.063
    9   2006-08-30      275          8      72  0.447   32.160
    10  2006-02-11      475          5      65  0.165   10.698
    

    UPDATE: Now that pandas 0.13 is out, another way to do this is df.query('line_race != 0').

    0 讨论(0)
  • 2020-11-22 04:40

    If you want to delete rows based on multiple values of the column, you could use:

    df[(df.line_race != 0) & (df.line_race != 10)]
    

    To drop all rows with values 0 and 10 for line_race.

    0 讨论(0)
  • 2020-11-22 04:40

    Though the previou answer are almost similar to what I am going to do, but using the index method does not require using another indexing method .loc(). It can be done in a similar but precise manner as

    df.drop(df.index[df['line_race'] == 0], inplace = True)
    
    0 讨论(0)
  • 2020-11-22 04:41

    The given answer is correct nontheless as someone above said you can use df.query('line_race != 0') which depending on your problem is much faster. Highly recommend.

    0 讨论(0)
提交回复
热议问题