Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Tilde Python Pandas DataFrame

#1
Tilde Python Pandas DataFrame

Python’s Tilde ~n operator is the bitwise negation operator: it takes the number n as binary number and “flips” all bits 0 to 1 and 1 to 0 to obtain the complement binary number. For example, the tilde operation ~1 becomes 0 and ~0 becomes 1 and ~101 becomes 010.

Read all about the Tilde operator in my detailed tutorial on this blog.

Sometimes, you’ll see the tilde operator in a Pandas DataFrame for indexing. Here’s an example:

import pandas as pd # Create a DataFrame
df = pd.DataFrame([{'User': 'Alice', 'Age': 22}, {'User': 'Bob', 'Age': 24}])
print(df) ''' User Age
0 Alice 22
1 Bob 24 ''' # Use Tilde to access all lines where user doesn't contain 'A'
df = df[~df['User'].str.contains('A')]
print(df) ''' User Age
1 Bob 24 '''

To improve your practical understanding, feel free to run this code in your browser in our interactive Python shell:

The tilde operator negates the Boolean values in the DataFrame: True becomes False and False becomes True.

You can see this in action when printing the result of different operations:

This is the original DataFrame in the code:

print(df) ''' User Age
0 Alice 22
1 Bob 24 '''

Now apply the contains operation to find all user names that contain the character 'A'.

print(df['User'].str.contains('A')) '''
0 True
1 False
Name: User, dtype: bool '''

The result is a DataFrame with Boolean values that indicate whether a user contains the character 'A' or not.

Let’s apply the Tilde operator on the result:

print(~df['User'].str.contains('A')) '''
0 False
1 True
Name: User, dtype: bool '''

Now, we use this DataFrame to access only those rows with users that don’t contain the character 'A'.

df = df[~df['User'].str.contains('A')]
print(df) ''' User Age
1 Bob 24 '''

Where to Go From Here?


Enough theory, let’s get some practice!

To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?

Practice projects is how you sharpen your saw in coding!

Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?

Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.

Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.

Join the free webinar now!



https://www.sickgaming.net/blog/2020/05/...dataframe/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] Making $65 per Hour on Upwork with Pandas xSicKxBot 0 1,323 05-24-2023, 08:16 PM
Last Post: xSicKxBot
  [Tut] Pandas Series Object – A Helpful Guide with Examples xSicKxBot 0 1,314 05-01-2023, 01:30 AM
Last Post: xSicKxBot
  [Tut] Python List of Tuples to DataFrame ? xSicKxBot 0 1,512 04-22-2023, 06:10 AM
Last Post: xSicKxBot
  [Tut] Dictionary of Lists to DataFrame – Python Conversion xSicKxBot 0 1,379 04-17-2023, 03:46 AM
Last Post: xSicKxBot
  [Tut] Pandas Boolean Indexing xSicKxBot 0 1,311 04-16-2023, 10:54 AM
Last Post: xSicKxBot
  [Tut] Python List of Dicts to Pandas DataFrame xSicKxBot 0 1,534 04-11-2023, 04:15 AM
Last Post: xSicKxBot
  [Tut] How to Create a DataFrame From Lists? xSicKxBot 0 1,225 12-17-2022, 03:17 PM
Last Post: xSicKxBot
  [Tut] How to Filter Data from an Excel File in Python with Pandas xSicKxBot 0 1,227 10-31-2022, 05:36 AM
Last Post: xSicKxBot
  [Tut] How to Convert Pandas DataFrame/Series to NumPy Array? xSicKxBot 0 1,220 10-24-2022, 02:13 PM
Last Post: xSicKxBot
  [Tut] How to Apply a Function to Each Cell in a Pandas DataFrame? xSicKxBot 0 1,076 08-23-2022, 05:25 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016