Is it possible to use a keyword name from **kwargs to filter my data frame?

Apologies if the title is a bit obscure, I am happy to change it..

Problem: I am trying to use a keyword name in the following code to filter by column name in a dataframe using pandas.

@staticmethod
def filter_json(json, col_filter, **kwargs):
    '''
        Convert and filter a JSON object into a dataframe
    '''
    df = pd.read_json(json).drop(col_filter, axis=1)
    for arg in kwargs:
        df = df[(df.arg.isin(kwargs[arg]))]
    return df

However I get error AttributeError: 'DataFrame' object has no attribute 'arg' because arg is not a valid column name (makes sense) at line df.arg.isin(kwargs[arg]))]

I am calling the method with the following…

filter_json(json_obj, MY_COL_FILTERS, IsOpen=['false', 0])

Meaning df.arg should essentially be df.IsOpen

Question: Is there a way to use arg as my column name (IsOpen) here? Rather then me having to input it manually as df.IsOpen

Answer

You can access columns with dataframe[columnname] notation as well: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html

Try:

for arg in kwargs:  # arg is 'IsOpen'
        df = df[(df[arg].isin(kwargs[arg]))]  # df['IsOpen'] is same as df.IsOpen

Leave a Reply

Your email address will not be published. Required fields are marked *