Search code examples
pythonpandasconditional-statementsisin

Python pandas, filter our data with isin and other conditions


I have tried to use below codes to filter out records from a dataframe, for data is either with item codes 04901 or 04940 and Year is in 2022. However, this is not work property, it only return nil data, not sure why this is not work as intended, this only return no record.

df.loc[(df['Item Code'].isin(['04901', '04940'])) & (df['Year'] == '2022')]

If anybody can get me a hit?


Solution

  • If years are integers use 2022 instead '2022':

    df[(df['Item Code'].isin(['04901', '04940'])) & (df['Year'] == 2022)]
    

    Or:

    df[df['Item Code'].isin(['04901', '04940']) & df['Year'].eq(2022)]