Open In App

Python | Data Comparison and Selection in Pandas

Last Updated : 17 Sep, 2018
Improve
Improve
Like Article
Like
Save
Share
Report

Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric Python packages. Pandas is one of those packages, and makes importing and analyzing data much easier.

The most important thing in Data Analysis is comparing values and selecting data accordingly. The “==” operator works for multiple values in a Pandas Data frame too. Following two examples will show how to compare and select data from a Pandas Data frame.

To download the CSV file used, Click Here.

Example #1: Comparing Data
In the following example, a data frame is made from a csv file. In the Gender Column, there are only 3 types of values (“Male”, “Female” or NaN). Every row of Gender column is compared to “Male” and a boolean series is returned after that.




# importing pandas package
import pandas as pd
  
# making data frame from csv file
data = pd.read_csv("employees.csv")
  
# storing boolean series in new
new = data["Gender"] == "Male"
  
# inserting new series in data frame
data["New"]= new
  
# display
data


Output:
As show in the output image, for Gender= “Male”, the value in New Column is True and for “Female” and NaN values it is False.


 
Example #2: Selecting Data
In the following example, the boolean series is passed to the data and only Rows having Gender=”Male” are returned.




# importing pandas package
import pandas as pd
  
# making data frame from csv file
data = pd.read_csv("employees.csv")
  
# storing boolean series in new
new = data["Gender"] != "Female"
  
# inserting new series in data frame
data["New"]= new
  
# display
data[new]
  
# OR 
# data[data["Gender"]=="Male"]
# Both are the same


Output:
As shown in the output image, Data frame having Gender=”Male” is returned.

Note: For NaN values, the boolean value is False.



Similar Reads

Limited rows selection with given column in Pandas | Python
Methods in Pandas like iloc[], iat[] are generally used to select the data from a given dataframe. In this article, we will learn how to select the limited rows with given columns with the help of these methods. Example 1: Select two columns # Import pandas package import pandas as pd # Define a dictionary containing employee data data = {'Name':['
2 min read
Python | Tkinter ttk.Checkbutton and comparison with simple Checkbutton
Tkinter is a GUI (Graphical User Interface) module which comes along with the Python itself. This module is widely used to create GUI applications. tkinter.ttk is used to create the GUI applications with the effects of modern graphics which cannot be achieved using only tkinter. Checkbutton is used to select multiple options. Checkbuttons can be cr
2 min read
Face Comparison Using Face++ and Python
Prerequisites: Python Programming Language Python is a high-level general-purpose language. It is used for multiple purposes like AI, Web Development, Web Scraping, etc. One such use of Python can be Face Comparison. A module name python-facepp can be used for doing the same. This module is for communicating with Face++ facial recognition service.
3 min read
Comparison between Lists and Array in Python
Python List Python programming language has four collection data types namely List, Tuple, Set, and Dictionary . A list is a mutable and ordered collection i.e., elements of the list can be changed and it maintains the order of insertion of its items. Because of the property of order maintaining, each element of the list has a fixed index and it pe
13 min read
Python | Excel File Comparison
Given Two Excel Files, We want to compare the values of each column row-wise after sorting the values and print the changed column name and row number and values change. Input : Two Excel files Output : Column name : 'location' and Row Number : 0 Column name : 'location' and Row Number : 3 Column name : 'date' and Row Number : 1 Code : Python code
1 min read
Python Object Comparison : "is" vs "=="
Both "is" and "==" are used for object comparison in Python. The operator "==" compares values of two objects, while "is" checks if two objects are same (In other words two references to same object). # Python program to demonstrate working of # "==" # Two different objects having same values x1 = [10, 20, 30] x2 = [10, 20, 30] # Comparis
2 min read
Python | Find Hotel Prices using Hotel price comparison API
Makcorps hotel API is used to get JSON data, to compare Hotel prices, ratings, and reviews from more than 200 websites including; Agoda.com, Hotels.com, Expedia and more. It is organized around GET Requests. One can use this API for free to get information for any hotel or any city regarding prices, ratings, reviews, historical prices and many othe
3 min read
Python | Consecutive String Comparison
Sometimes, while working with data, we can have a problem in which we need to perform comparison between a string and it's next element in a list and return all strings whose next element is similar list. Let's discuss certain ways in which this task can be performed. Method #1 : Using zip() + loop This is one way in which this task can be performe
3 min read
Comparison of Python with Other Programming Languages
Python is an easily adaptable programming language that offers a lot of features. Its concise syntax and open-source nature promote readability and implementation of programs which makes it the fastest-growing programming language in current times. Python has various other advantages which give it an edge over other popular programming languages su
3 min read
Case-insensitive string comparison in Python
We generally use Python lists to store items. An online shopping application may contain a list of items in it so that the user can search the item from the list of items. For example, Our shopping application has a list of laptops it sells. List contains many brands and one of them is 'Lenovo'. If we want to buy a laptop of Lenovo brand we go to t
4 min read