Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tilde sign in python dataframe

Tags:

python

pandas

I'm new to python/pandas and came across a code snippet.

df = df[~df['InvoiceNo'].str.contains('C')] 

Would be much obliged if I could know what is the tilde sign's usage in this context?

like image 580
Nirojan Selvanathan Avatar asked Sep 05 '17 11:09

Nirojan Selvanathan


People also ask

What is tilde in Dataframe Python?

tilde ~ is a bitwise operator. If the operand is 1, it returns 0, and if 0, it returns 1. So you will get the InvoiceNo values in the df that does not contain the string 'C'

What does the tilde mean in Python?

The bitwise operator ~ (pronounced as tilde) is a complement operator. It takes one bit operand and returns its complement. If the operand is 1, it returns 0, and if it is 0, it returns 1.

Which of these symbols can be used to tell pandas to perform the reverse of a filter condition?

Pandas: Using the tilde operator to return inverse data with two filters.


2 Answers

It means bitwise not, inversing boolean mask - Falses to Trues and Trues to Falses.

Sample:

df = pd.DataFrame({'InvoiceNo': ['aaC','ff','lC'],                    'a':[1,2,5]}) print (df)   InvoiceNo  a 0       aaC  1 1        ff  2 2        lC  5  #check if column contains C print (df['InvoiceNo'].str.contains('C')) 0     True 1    False 2     True Name: InvoiceNo, dtype: bool  #inversing mask print (~df['InvoiceNo'].str.contains('C')) 0    False 1     True 2    False Name: InvoiceNo, dtype: bool 

Filter by boolean indexing:

df = df[~df['InvoiceNo'].str.contains('C')] print (df)   InvoiceNo  a 1        ff  2 

So output is all rows of DataFrame, which not contains C in column InvoiceNo.

like image 94
jezrael Avatar answered Sep 17 '22 14:09

jezrael


It's used to invert boolean Series, see pandas-doc.

like image 37
RobinFrcd Avatar answered Sep 19 '22 14:09

RobinFrcd