Python - Tilde Sign (~) in Pandas DataFrame

In this article, we are going to learn what is the significance of the tilde sign (~) in pandas?
Submitted by Pranit Sharma, on August 22, 2022

Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.

Tilde Sign (~) in Pandas DataFrame

Tilde sign (~) in pandas is used when we work with Boolean values. In programming, we sometimes use some specific values that only have two values, either True or False. These values are known as Boolean values.

This tilde sign works as a negation operator that means is used to reverse the Boolean values.

For example, if we assign a tilde sign before any statement which is supposed to return True then this tilde sign converts True to False and vice versa.

Let us understand with the help of an example,

Python program to demonstrate the significance of the tilde sign (~) in pandas

# Importing pandas package import pandas as pd # Creating a dictionary d = { 'Actor':['Amit','Nawaz','Manoj'], 'Web-Series':['Avrodh','Sacred Games','The Family Man'], 'Platform':['Sony liv','Netflix','Prime'] } # Creating a DataFrame df = pd.DataFrame(d) # Display Original DataFrame print("Created DataFrame:\n",df,"\n") # Check if platform column contains Netflix result = df['Platform'].str.contains('Netflix') # Display original result print("Result:\n",result) # Now inverting the result result = ~df['Platform'].str.contains('Netflix') # Display modified result print("Modified Result:\n",result)

Output

The output of the above program is:

Example: Tilde Sign (~) in Pandas DataFrame

Python Pandas Programs »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement



Copyright © 2024 www.includehelp.com. All rights reserved.