How do you delete rows with a certain object in pandas, python?

后端 未结 2 2026
陌清茗
陌清茗 2021-01-29 11:39

I have a column in my data that contains these kind of values

2

2

yes

2

yes

In python pandas how would I identify the entire row

相关标签:
2条回答
  • 2021-01-29 11:55

    Here's a way to solve this. I've included code to build the DataFrame, find the string values and then drop them. df.drop returns a modified copy of the original df unless you specify inplace=True, so if you want, you can assign the original name of the df df = df.drop..., or use the inplace argument.

    import pandas as pd
    import numpy as np
    # build the df
    df = pd.DataFrame({'col_name' : [2, 2, 'yes', 2, 'yes']})
    print(df)
    
      col_name
    0        2
    1        2
    2      yes
    3        2
    4      yes
    
    # ask what are the types in column
    df.col_name.apply(type)
    
    0    <class 'int'>
    1    <class 'int'>
    2    <class 'str'>
    3    <class 'int'>
    4    <class 'str'>
    Name: col_name, dtype: object
    
    # you can see that the strings are in row 2 and 4
    # and drop them like this
    df.drop([2, 4], axis='rows')
    
      col_name
    0        2
    1        2
    3        2
    
    0 讨论(0)
  • 2021-01-29 12:01

    IIUC:

    df = df[~pd.to_numeric(df['col'], errors='coerce').isna()]
    

    or

    df = df[pd.to_numeric(df['col'], errors='coerce').notna()]
    
    0 讨论(0)
提交回复
热议问题