Dynamic filter for Pandas DataFrame
I have to filter a pandas dataframe with dynamic rows. The approach I am taking is creating a temp dataframe with the help of loop and then finally using concat to join the dataframes. This process works but it takes lot of time and probably this is an in-efficient way of handling this. I am giving my codes and please help me if there is a better way to handle this. I am providing a sample data with sample array. The array would be generated dynamically.
import pandas as pd ##Sample DataFrame df = pd.read_csv("https://raw.githubusercontent.com/justmarkham/pandas-videos/master/data/ufo.csv") ##Sample Filters filters = ['NY', 'NJ', 'CO', 'KS', 'ND'] finaldf = pd.DataFrame() for x in range(len(filters)): temp = df[df.State==filters[x]] finaldf = pd.concat([finaldf,temp]) print(finaldf.State.unique())
This gives the expected results.