Monday, August 24, 2020

Python — Filtering data with Pandas Dataframe

If you are working as Python developer where you have to accomplished a lot of data cleansing stuffs. One of the data cleansing stuff is to remove unwanted data from your dataframe. Pandas is one of the most important packages that makes importing and analyzing data much easier with the help of its strong library.

For analyzing data, a programmer requires a lot of filtering operations. Pandas provide many methods to filter a Data frame and Dataframe.query() is one of them.

To understand filtering feature of Pandas, we are creating some sample data by using list feature of Python.

In this example, dataframe has been filtered on multiple conditions.

# Import pandas library

import pandas as pd

 

# intialise data of lists.

data = {'Name':['Ryan Arjun', 'Kimmy Wang', 'Rose Gray', 'Will Smith'],

        'Age':[20, 21, 19, 18],

        'Country':['India','Taiwan','Canada','Greenland'],

        'Sex':['Male','Female','Female','Male']}

 

# Create DataFrame

df = pd.DataFrame(data)

 

#show data in the dataframe

df

=======================================

   Age |   Country |       Name   |  Sex

--------------------------------------- 

0   20 |     India | Ryan Arjun   |  Male

1   21 |    Taiwan | Kimmy Wang   |Female

2   19 |    Canada |  Rose Gray   |Female

3   18 | Greenland | Will Smith   | Male

=======================================

 

# filtering with query method

# Where sex must be male

# and Country must be India

# and age must be greater than 15

df.query('Sex =="Male" and Country =="India" and Age>15', inplace = True)

 

#show data in the dataframe

df

 

===================================

Age | Country   |     Name  | Sex

-----------------------------------

20  |India      |Ryan Arjun | Male

===================================

By using query feature of pandas in Python can save a lot of data processing time because we can use multiple filters conditions in a single go.

To learn more, please follow us -

http://www.sql-datatools.com

To Learn more, please visit our YouTube channel at - 

http://www.youtube.com/c/Sql-datatools

To Learn more, please visit our Instagram account at -

https://www.instagram.com/asp.mukesh/

To Learn more, please visit our twitter account at -

https://twitter.com/macxima

To Learn more, please visit our Medium account at -

https://medium.com/@macxima

3 comments:

  1. The article provides a practical introduction to filtering data using the Pandas DataFrame query() method, demonstrating how multiple conditions can be applied efficiently to clean and process datasets. By using a simple real-world example, it shows how Pandas simplifies data manipulation while improving code readability and reducing processing effort. The explanation makes it easier for beginners and developers alike to understand how effective data filtering contributes to accurate and efficient data analysis.

    Pandas is one of the most widely used Python libraries for data manipulation, offering powerful capabilities for filtering, transforming, and analyzing structured datasets. Learning features such as DataFrames, querying, indexing, and data cleansing enables developers to build efficient data processing workflows for analytics and reporting. Those looking to master these capabilities can explore Pandas Training, which provides hands-on experience with practical data manipulation techniques.

    ReplyDelete
  2. Building a strong foundation in Pandas naturally leads to broader expertise in data analysis, where cleaned and transformed datasets are converted into meaningful business insights. Understanding analytical workflows helps professionals make informed decisions using real-world data while supporting machine learning and business intelligence applications. Learners can further strengthen these skills through Data Analysis Training, covering essential concepts in data exploration, statistical analysis, and Python-based analytics.

    ReplyDelete
  3. Readers interested in expanding their Python expertise beyond data manipulation can also explore Python Training, which introduces modern tools, frameworks, and best practices for analytics, automation, and software development.

    ReplyDelete