Remove rows from pandas DataFrame based on condition

后端 未结 1 1155
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-01 03:45

I am a newbie to pandas so please forgive the newbie question!

I have the following code;

import pandas as pd

pet_names = [\"Name\",\"Species\"
\"Ja         


        
相关标签:
1条回答
  • 2021-01-01 04:37

    General boolean indexing

    df[df['Species'] != 'Cat']
    # df[df['Species'].ne('Cat')]
    
      Index    Name Species
    1     1    Jill     Dog
    3     3   Harry     Dog
    4     4  Hannah     Dog
    

    df.query

    df.query("Species != 'Cat'")
    
      Index    Name Species
    1     1    Jill     Dog
    3     3   Harry     Dog
    4     4  Hannah     Dog
    

    For information on the pd.eval() family of functions, their features and use cases, please visit Dynamic Expression Evaluation in pandas using pd.eval().


    df.isin

    df[~df['Species'].isin(['Cat'])]
    
      Index    Name Species
    1     1    Jill     Dog
    3     3   Harry     Dog
    4     4  Hannah     Dog
    
    0 讨论(0)
提交回复
热议问题