How to Remove Rows from Pandas Data Frame that Contains any String in a Particular Column

后端 未结 4 763
轻奢々
轻奢々 2021-01-21 18:04

I have CSV data in the following format:

+-------------+-------------+-------+
|  Location   | Num of Reps | Sales |
+-------------+-------------+-------+
| 7589         


        
相关标签:
4条回答
  • 2021-01-21 18:14

    You can use pd.to_numeric to coerce non numeric values to nan and then filter based on if the Location is nan:

    df[pd.to_numeric(df.Location, errors='coerce').notnull()]
    
    #Location  Num of Reps  Sales
    #0  75894            3     12
    #2  75286            7     24
    #4  27659            3     17
    
    0 讨论(0)
  • 2021-01-21 18:21
    In [139]: df[~df.Location.str.contains('\D')]
    Out[139]:
      Location  Num of Reps  Sales
    0    75894            3     12
    2    75286            7     24
    4    27659            3     17
    
    0 讨论(0)
  • 2021-01-21 18:22

    Or you could do

    df[df['Location'].str.isnumeric()]
    
    
      Location  Num of Reps  Sales
    0    75894            3     12
    2    75286            7     24
    4    27659            3     17
    
    0 讨论(0)
  • 2021-01-21 18:29
    df[df['Location'].str.isdigit()]
    
    
      Location  Num of Reps  Sales
    0    75894            3     12
    2    75286            7     24
    4    27659            3     17
    
    0 讨论(0)
提交回复
热议问题