05-17-2020, 03:40 AM
Tilde Python Pandas DataFrame
<div><p><strong>Python’s Tilde <code>~n</code> operator is the <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Bitwise_operation#NOT" target="_blank">bitwise negation operator</a>: it takes the number <code>n</code> as binary number and “flips” all bits <code>0 to 1</code> and <code>1 to 0</code> to obtain the complement binary number. For example, the tilde operation <code>~1</code> becomes <code>0</code> and <code>~0</code> becomes <code>1</code> and <code>~101</code> becomes <code>010</code>.</strong></p>
<p><a href="https://blog.finxter.com/tilde-python/" target="_blank" rel="noreferrer noopener"><em>Read all about the Tilde operator in my detailed tutorial on this blog.</em></a></p>
<p>Sometimes, you’ll see the tilde operator in a <a href="https://blog.finxter.com/pandas-cheat-sheets/" target="_blank" rel="noreferrer noopener">Pandas DataFrame</a> for indexing. Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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 '''</pre>
<p><em>To improve your practical understanding, feel free to run this code in your browser in our interactive Python shell:</em></p>
<p> <iframe src="https://repl.it/@finxter/tildepythonpandas?lite=true" scrolling="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals" width="100%" height="800px" frameborder="no"></iframe> </p>
<p>The tilde operator negates the <a href="https://blog.finxter.com/python-cheat-sheet/" target="_blank" rel="noreferrer noopener">Boolean </a>values in the DataFrame: <code>True</code> becomes <code>False</code> and <code>False</code> becomes <code>True</code>. </p>
<p>You can see this in action when <a href="https://blog.finxter.com/print-a-list-of-list-in-python/" target="_blank" rel="noreferrer noopener">printing</a> the result of different operations:</p>
<p>This is the original DataFrame in the code:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(df) ''' User Age
0 Alice 22
1 Bob 24 '''</pre>
<p>Now apply the <a href="https://blog.finxter.com/python-string-contains/" target="_blank" rel="noreferrer noopener">contains </a>operation to find all user names that contain the character <code>'A'</code>. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(df['User'].str.contains('A')) '''
0 True
1 False
Name: User, dtype: bool '''</pre>
<p>The result is a DataFrame with Boolean values that indicate whether a user contains the character <code>'A'</code> or not. </p>
<p>Let’s apply the Tilde operator on the result:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(~df['User'].str.contains('A')) '''
0 False
1 True
Name: User, dtype: bool '''</pre>
<p>Now, we use this DataFrame to access only those rows with users that don’t contain the character <code>'A'</code>. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">df = df[~df['User'].str.contains('A')]
print(df) ''' User Age
1 Bob 24 '''</pre>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>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?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>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.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>
https://www.sickgaming.net/blog/2020/05/...dataframe/
<div><p><strong>Python’s Tilde <code>~n</code> operator is the <a rel="noreferrer noopener" href="https://en.wikipedia.org/wiki/Bitwise_operation#NOT" target="_blank">bitwise negation operator</a>: it takes the number <code>n</code> as binary number and “flips” all bits <code>0 to 1</code> and <code>1 to 0</code> to obtain the complement binary number. For example, the tilde operation <code>~1</code> becomes <code>0</code> and <code>~0</code> becomes <code>1</code> and <code>~101</code> becomes <code>010</code>.</strong></p>
<p><a href="https://blog.finxter.com/tilde-python/" target="_blank" rel="noreferrer noopener"><em>Read all about the Tilde operator in my detailed tutorial on this blog.</em></a></p>
<p>Sometimes, you’ll see the tilde operator in a <a href="https://blog.finxter.com/pandas-cheat-sheets/" target="_blank" rel="noreferrer noopener">Pandas DataFrame</a> for indexing. Here’s an example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">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 '''</pre>
<p><em>To improve your practical understanding, feel free to run this code in your browser in our interactive Python shell:</em></p>
<p> <iframe src="https://repl.it/@finxter/tildepythonpandas?lite=true" scrolling="no" allowtransparency="true" allowfullscreen="true" sandbox="allow-forms allow-pointer-lock allow-popups allow-same-origin allow-scripts allow-modals" width="100%" height="800px" frameborder="no"></iframe> </p>
<p>The tilde operator negates the <a href="https://blog.finxter.com/python-cheat-sheet/" target="_blank" rel="noreferrer noopener">Boolean </a>values in the DataFrame: <code>True</code> becomes <code>False</code> and <code>False</code> becomes <code>True</code>. </p>
<p>You can see this in action when <a href="https://blog.finxter.com/print-a-list-of-list-in-python/" target="_blank" rel="noreferrer noopener">printing</a> the result of different operations:</p>
<p>This is the original DataFrame in the code:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(df) ''' User Age
0 Alice 22
1 Bob 24 '''</pre>
<p>Now apply the <a href="https://blog.finxter.com/python-string-contains/" target="_blank" rel="noreferrer noopener">contains </a>operation to find all user names that contain the character <code>'A'</code>. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(df['User'].str.contains('A')) '''
0 True
1 False
Name: User, dtype: bool '''</pre>
<p>The result is a DataFrame with Boolean values that indicate whether a user contains the character <code>'A'</code> or not. </p>
<p>Let’s apply the Tilde operator on the result:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">print(~df['User'].str.contains('A')) '''
0 False
1 True
Name: User, dtype: bool '''</pre>
<p>Now, we use this DataFrame to access only those rows with users that don’t contain the character <code>'A'</code>. </p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">df = df[~df['User'].str.contains('A')]
print(df) ''' User Age
1 Bob 24 '''</pre>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>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?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>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.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>
https://www.sickgaming.net/blog/2020/05/...dataframe/