

You can filter on specific dates, or on any of the date selectors that Pandas makes available.

Pandas also makes it very easy to filter on dates. This returns: Date Region Type Units Sales3 East Children's Clothing 26.0 8325 North Women's Clothing 33.0 6277 East Women's Clothing 29.0 6099 East Children's Clothing 34.0 37414 East Men's Clothing 30.0 360(299, 5) Date Region Type Units Sales0 East Children's Clothing 18.0 3061 North Children's Clothing 14.0 4482 South Women's Clothing 17.0 4253 East Children's Clothing 26.0 8325 North Women's Clothing 33.0 627(790, 5) Filter a Dataframe Based on Dates Then youll do the same with an or operator: and_operator = df > 300) & (df > 20)]print(and_operator.head())print(and_operator.shape)or_operator = df > 300) | (df > 20)]print(or_operator.head())print(or_operator.shape) First, youll select rows where sales are greater than 300 and units are greater than 20. If you want to filter based on more than one condition, you can use the ampersand (&) operator or the pipe (|) operator, for and and or respectively. We can see from the shape method that 352 rows have been filtered out of the dataset.Ĭheck out some other Python tutorials on datagy, including our complete guide to styling Pandas and our comprehensive overview of Pivot Tables in Pandas! Filtering a Dataframe based on Multiple Conditions This returns: Date Region Type Units Sales0 East Children's Clothing 18.0 3061 North Children's Clothing 14.0 4482 South Women's Clothing 17.0 4253 East Children's Clothing 26.0 8325 North Women's Clothing 33.0 627(648, 5) Select Dataframe Values Greater Than Or Less Thanįor example, if you wanted to select rows where sales were over 300, you could write: greater_than = df > 300]print(greater_than.head())print(greater_than.shape) This can be accomplished using the index chain method. Pandas makes it incredibly easy to select data by a column value. This returns: Date Region Type Units Sales0 East Children's Clothing 18.0 3061 North Children's Clothing 14.0 4482 South Women's Clothing 17.0 4253 East Children's Clothing 26.0 8324 West Women's Clothing 3.0 33 Filter Pandas Dataframe by Column Value We used the parse_dates parameter to ensure that the Dates column was read as datetime. import pandas as pddf = pd.read_excel('', parse_dates=)print(df.head()) Lets begin by loading a sample dataframe that well use throughout the tutorial.

