Selecting multiple (neighboring) rows conditionally

前端 未结 2 1912
無奈伤痛
無奈伤痛 2021-01-21 06:43

I\'d like to return the rows which qualify to a certain condition. I can do this for a single row, but I need this for multiple rows combined. For example \'light green\' qualif

2条回答
  •  难免孤独
    2021-01-21 07:15

    I am not too sure if I understood your question correctly, but if you are looking to put multiple conditions within a dataframe, you can consider this approach:

    new_df = df[(df["X"] > 0) & (df["Y"] < 0)]

    The & condition is for AND, while replacing that with | is for OR condition. Do remember to put the different conditions in ().

    Lastly, if you want to remove duplicates, you can use this

    new_df.drop_duplicates()

    You can find more information about this function at here: http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.drop_duplicates.html

    Hope my answer is useful to you.

提交回复
热议问题