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

后端 未结 2 2016
陌清茗
陌清茗 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    
    1    
    2    
    3    
    4    
    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
    

提交回复
热议问题