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
I am calling the method with the following…
filter_json(json_obj, MY_COL_FILTERS, IsOpen=['false', 0])
df.arg should essentially be
Question: Is there a way to use
arg as my column name (
IsOpen) here? Rather then me having to input it manually as
You can access columns with
dataframe[columnname] notation as well: https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html
for arg in kwargs: # arg is 'IsOpen' df = df[(df[arg].isin(kwargs[arg]))] # df['IsOpen'] is same as df.IsOpen