How to filter dataframe based on a dictionary?

 

# Import Libs
import pandas as pd

# data ingestion
df = pd.DataFrame({
    'AREA': ['EXCLUIDO','AREA I','AREA II','AREA III','AREA IV','AREA V','AREA VI','AREA VII','AREA VIII','AREA IX'],
    'test': ['XXX','AAA','BBB','CCC','DDD','EEE','FFF','GGG','HHH','III']
})

# Dictionary
dictionary = {
    0:'EXCLUIDO',
    1:'AREA I',
    2:'AREA II',
    3:'AREA III',
    4:'AREA IV',
    5:'AREA V',
    6:'AREA VI',
    7:'AREA VII',
    8:'AREA VIII',
    9:'AREA IX'
}

# filter based on dictionary
filter = (df['AREA'] == dictionary[2])|(df['AREA'] == dictionary[4])
print(df.loc[filter])

Leave a Reply