Search code examples
pythonpandasargs

Python Filter Dataframe with an unknown list of values with Kwargs


Im new to coding but I wanted to see if there was a way to filter a dataframe with an unknown number of values. What I have so far

df[(df['column'].astype(str)=='one')|(df['column'].astype(str)=='two')|(df['column'].astype(str)=='three')]

This filters a dataframe just fine but I have an unknown number of values. What I wanted would be something like:

x=['one','two','three']
y=['one','two','three','four']
z=['one','two','three','four','five']

for arg in *args:
     df=df[(df['column'].astype(str)==arg )]

But this does not seem to work. Does anyone have any thoughts?


Solution

  • Use isin

    >>> df[df['column'].isin(x)]  # or y or z