Adventures in Machine Learning

Unlocking the Power of Pandas: Filtering Selecting and Querying DataFrames

Unlocking the Power of Pandas- A Guide to Filtering DataFrames and Creating Structures

If you’re a data analyst and a fan of Python, you’ve probably already heard of pandas. Pandas is an open-source Python library used for data manipulation and analysis.

It provides numerous data structures and functions to handle various data types and operations. In this article, we will be covering two essential topics – filtering pandas DataFrame by column values and DataFrame structure, and creation.

Filtering a pandas DataFrame by column values

The ability to filter data is essential in data analysis. Pandas makes it effortless to filter data based on columns.

You can extract data by a single value or a list of values. We will explore three examples in more detail.

Example 1: Filter Based on One Column

Consider the following DataFrame:

![DataFrame](https://i.imgur.com/84bu4BX.png)

We want to filter the data based on the value in the “points” column. We can use the query function to achieve the following:

“`

df.query(‘points == 76’)

“`

This will return a new DataFrame containing only the rows where the value in the “points” column equals 76.

You can also use the loc function to filter the data:

“`

df.loc[df[‘points’]==76]

“`

Example 2: Filter Based on Multiple Columns

In some cases, you may want to filter data based on multiple conditions or values in different columns. Consider the following DataFrame:

![DataFrame](https://i.imgur.com/t1fSmAP.png)

Suppose you want to filter the data based on the values in the “points” and “rebounds” columns.

You can use the query function to achieve this:

“`

df.query(‘points>=20 and rebounds>5’)

“`

This will return a new DataFrame containing only the rows where the values in both the “points” and “rebounds” columns meet the conditions. Example 3: Filter Based on Values in a List

You may also want to filter data based on values in a list.

Consider the following DataFrame:

![DataFrame](https://i.imgur.com/MuqeRjZ.png)

Suppose you want to filter the data based on the values in the “points” column that are not in a given list of values. You can use the query function to achieve this:

“`

df.query(‘points not in [5, 10, 15]’)

“`

This will return a new DataFrame containing only the rows where the values in the “points” column are not in the list of values [5, 10, 15].

DataFrame structure and creation

Now that we have explored filtering data in pandas, let’s move on to discussing

DataFrame structure and creation.

Importing pandas

To use pandas, we must first import it. We typically import pandas with the alias pd:

“`

import pandas as pd

“`

Creating a DataFrame

The most common way of creating a DataFrame is using a dictionary. A dictionary is a collection of key-value pairs.

We can create a DataFrame from a dictionary where the keys become the column names, and the values become the data. Consider the following dictionary:

“`

data = {‘name’: [‘Alice’, ‘Bob’, ‘Charlie’, ‘Dave’], ‘age’: [25, 27, 23, 31], ‘city’: [‘New York’, ‘Paris’, ‘London’, ‘Madrid’]}

“`

We can create an empty DataFrame and add the data using the from_dict function:

“`

df = pd.DataFrame().from_dict(data)

“`

This creates the following DataFrame:

![DataFrame](https://i.imgur.com/gtRsBqm.png)

Viewing a DataFrame

We can view the DataFrame using the head function, which shows the first 5 rows by default. Alternatively, we can use the tail function to show the last 5 rows.

Consider the following DataFrame:

![DataFrame](https://i.imgur.com/o3NxB4M.png)

“`

df.head()

“`

This will show the first 5 rows of the DataFrame:

![DataFrame](https://i.imgur.com/c5cRhL5.png)

“`

df.tail()

“`

This will show the last 5 rows of the DataFrame:

![DataFrame](https://i.imgur.com/k4V8cHT.png)

Conclusion

In conclusion, pandas is a powerful library for data manipulation and analysis. Being able to filter DataFrames by column values is an essential skill in data analysis.

We have explored different examples of how to filter data based on single values, multiple values, and values in a list. Creating DataFrames in pandas is also straightforward, and we can use a dictionary to add data.

Pandas provides numerous functions to manipulate and analyze the data once we have created a DataFrame. We hope this guide will help you unlock the potential of pandas and make your data analysis tasks smoother and more straightforward.

Primary Methods for Filtering DataFrames

Data filtering is an integral part of data manipulation and analysis. In addition to filtering by column values, pandas provides several methods to filter DataFrames, including boolean indexing and the isin() method.

Let’s dive into these filtering methods in more detail. Example 1: Filtering by Boolean Indexing

Boolean indexing is a powerful filtering method in pandas that uses a combination of comparison operators and logical operators to filter data.

The technique involves creating a boolean mask, which is a Series or DataFrame of True and False values that match the DataFrame’s shape. True values in the boolean mask are used to select data, while False values are ignored.

Consider the following DataFrame:

![DataFrame](https://i.imgur.com/7wpsMon.png)

Suppose we want to filter the DataFrame based on values in the “points” column that are greater than 80. We can create a boolean mask using comparison operators and the “points” column, and use the loc function to filter the data:

“`

boolean_mask = df[‘points’] > 80

df_filtered = df.loc[boolean_mask]

“`

This will return a new DataFrame with only the rows where the “points” column value is greater than 80:

![DataFrame](https://i.imgur.com/Qs0Akz2.png)

Boolean indexing is also useful when filtering DataFrames based on multiple conditions.

For example, suppose we want to filter out players with points greater than 80 and rebounds greater than 10. We can use the logical operator “&” to combine the boolean conditions in the boolean mask:

“`

boolean_mask = (df[‘points’] > 80) & (df[‘rebounds’] > 10)

df_filtered = df.loc[boolean_mask]

“`

This will return a new DataFrame with only the rows where both conditions are met:

![DataFrame](https://i.imgur.com/Ew4Oirn.png)

Example 2: Filtering by the isin() Method

The isin() method is used to filter data based on whether or not a value is in a list of values.

For example, suppose we have the following DataFrame:

![DataFrame](https://i.imgur.com/ZYksaKF.png)

Suppose we want to filter the DataFrame based on the values in the “points” column that are not in the list [25, 40, 60]. We can use the isin() method to filter the data:

“`

values_in_list = [25, 40, 60]

df_filtered = df.loc[~df[‘points’].isin(values_in_list)]

“`

This will return a new DataFrame with only the rows where the values in the “points” column are not in the list of values:

![DataFrame](https://i.imgur.com/GgSQ8vT.png)

Selecting Data in DataFrames

In addition to filtering data, pandas also provides two primary methods for selecting data in DataFrames – selecting by label and selecting by position. Example 1: Selecting by Label

Selecting by label, also known as label-based indexing, is a method of indexing rows and columns using their labels.

The loc function is used for label-based indexing in pandas. Consider the following DataFrame:

![DataFrame](https://i.imgur.com/JkOpsa6.png)

Suppose we want to select a specific row using its label.

We can use the loc function to select the row:

“`

selected_row = df.loc[2]

“`

This will return the “Charlie” row:

![DataFrame](https://i.imgur.com/86K0L8f.png)

We can also use the loc function to select specific columns and rows by their labels. For example, suppose we want to select only the “age” and “city” columns for the “Bob” and “Charlie” rows.

We can use the loc function to select the data:

“`

selected_data = df.loc[[1, 2], [‘age’, ‘city’]]

“`

This will return a new DataFrame with only the selected columns and rows:

![DataFrame](https://i.imgur.com/px8P4Gl.png)

Example 2: Selecting by Position

Selecting by position, also known as position-based indexing, is a method of indexing rows and columns using their integer positions. The iloc function is used for position-based indexing in pandas.

Consider the following DataFrame:

![DataFrame](https://i.imgur.com/qe1lVOC.png)

Suppose we want to select data using integer positions. We can use the iloc function to select the data:

“`

selected_data = df.iloc[1:3, 0:2]

“`

This will return a new DataFrame with only the selected rows and columns:

![DataFrame](https://i.imgur.com/UbFfJyM.png)

In conclusion, pandas provides several powerful methods for filtering and selecting data in DataFrames.

Boolean indexing is a flexible and efficient method for filtering data based on multiple conditions. The isin() method is ideal for filtering data based on a list of values.

Label-based indexing and position-based indexing are both useful for selecting specific rows and columns in a DataFrame. Combining these techniques can make data analysis in pandas even more efficient and powerful.

Querying DataFrames

Querying DataFrames is a common operation when working with data in pandas. It is the process of filtering and manipulating data based on conditions or expressions.

While we have already discussed filtering DataFrames with boolean indexing and the isin() method, pandas also provides two built-in methods for querying DataFrames – the .query() and .eval() methods. Example 1: Using the .query Method

The .query() method is a convenient way to filter DataFrames based on boolean expressions.

It is ideal for filtering large DataFrames with complex conditions. Consider the following DataFrame:

![DataFrame](https://i.imgur.com/RVSks8M.png)

Suppose we want to filter the DataFrame based on values in the “points” column that are greater than 80.

We can use the .query() method to achieve this:

“`

df_filtered = df.query(‘points > 80’)

“`

This will return a new DataFrame with only the rows where the “points” column value is greater than 80:

![DataFrame](https://i.imgur.com/twBKRJ9.png)

The .query() method also supports chaining multiple conditions together using logical operators. For example, suppose we want to filter out players with points outside the range [80, 90].

We can use the .query() method to achieve this:

“`

df_filtered = df.query(‘points >= 80 and points <= 90')

“`

This will return a new DataFrame with only the rows where the “points” column value is between 80 and 90:

![DataFrame](https://i.imgur.com/GXjQ8Wp.png)

Example 2: Using the .eval Method

The .eval() method is used to evaluate an expression on a DataFrame or Series. It is useful for creating new columns based on calculations of existing columns.

Consider the following DataFrame:

![DataFrame](https://i.imgur.com/Tm0l6dC.png)

Suppose we want to calculate the total points earned by each player based on their “points” and “assists” columns. We can use the .eval() method to create a new column:

“`

df.eval(‘total_points = points + assists’, inplace=True)

“`

This will add a new column “total_points” to the DataFrame with each row’s total points:

![DataFrame](https://i.imgur.com/EQ1vHNj.png)

The .eval() method can also be used to assign new values to existing columns.

For example, suppose we want to convert the “points” column from points to dollars by multiplying it by 10. We can use the .eval() method to achieve this:

“`

df.eval(‘points = points * 10’, inplace=True)

“`

This will modify the “points” column to reflect the new values:

![DataFrame](https://i.imgur.com/xUyqzdb.png)

In conclusion, querying DataFrames is an essential operation in data manipulation and analysis.

The .query() and .eval() methods are powerful built-in methods that make it easier to filter and manipulate DataFrames based on complex conditions and calculations. By leveraging these methods, you can produce efficient and readable code that maximizes the power of pandas.

In conclusion, pandas is a powerful data manipulation and analysis library that provides several built-in methods for filtering, selecting, and querying DataFrames. By mastering these techniques, you can perform complex data analysis tasks with ease and efficiency.

We have discussed the primary methods for filtering DataFrames, including boolean indexing and the isin() method. We have also examined how to select data in DataFrames using label-based and position-based indexing.

Finally, we have explored the .query() and .eval() methods, which are powerful tools for filtering and manipulating data based on complex conditions and expressions. By leveraging these techniques, you can make data analysis tasks more efficient and effective, providing insights that drive informed decision-making.

Popular Posts