Home > OS >  How to use key:value pairs from a dict to build a pandas filter
How to use key:value pairs from a dict to build a pandas filter

Time:04-15

I have a dictionary that can take any length, for example, this one:

dic = {'A':'Rome','B':'Japan','C':'EUA'}

and I want to build a function do filter a dataframe using the dict above as parameter, like this:

def filter(dic, df):
    for k,v in dic.items():
        x=df[(df[k]==v) & (df[k]==v) & (df[k]==v)]
    return x

If I had to hard code the filter above would be:

df[(df['A']=='Rome') & (df['B']=='Japan') & (df['C']=='EUA')]

The problem I am facing:

  1. The dictionary doesn't have a fixed length so the number of parameter will change every time.

  2. The code above is not iterating correctly over the dictionary

How can I make that filter function work?

CodePudding user response:

Assuming that the dic dictionary contains all columns, simply use:

df[df.eq(dic).all(1)]

else, use:

df[df[list(dic)].eq(dic).all(1)]

Example:

dic = {'A':'Rome','B':'Japan','C':'EUA'}

df = pd.DataFrame({'A': ['Rome', 'Rome', 'Milan'],
                   'B': ['Japan', 'Italy', 'Japan'],
                   'C': ['EUA', 'Italy', 'Japan'],
                   'D': [1,2,3]
                  })

df[df[list(dic)].eq(dic).all(1)]

output:

      A      B    C  D
0  Rome  Japan  EUA  1
  • Related