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 -
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 -
To Learn more, please visit our Medium account at -
No comments:
Post a Comment