{"id":112998,"date":"2020-05-16T18:19:40","date_gmt":"2020-05-16T18:19:40","guid":{"rendered":"https:\/\/blog.finxter.com\/?p=8616"},"modified":"2020-05-16T18:19:40","modified_gmt":"2020-05-16T18:19:40","slug":"tilde-python-pandas-dataframe","status":"publish","type":"post","link":"https:\/\/sickgaming.net\/blog\/2020\/05\/16\/tilde-python-pandas-dataframe\/","title":{"rendered":"Tilde Python Pandas DataFrame"},"content":{"rendered":"<p><strong>Python&#8217;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 &#8220;flips&#8221; 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>\n<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>\n<p>Sometimes, you&#8217;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&#8217;s an example:<\/p>\n<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\ndf = pd.DataFrame([{'User': 'Alice', 'Age': 22}, {'User': 'Bob', 'Age': 24}])\nprint(df) ''' User Age\n0 Alice 22\n1 Bob 24 ''' # Use Tilde to access all lines where user doesn't contain 'A'\ndf = df[~df['User'].str.contains('A')]\nprint(df) ''' User Age\n1 Bob 24 '''<\/pre>\n<p><em>To improve your practical understanding, feel free to run this code in your browser in our interactive Python shell:<\/em><\/p>\n<p> <iframe loading=\"lazy\" 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>\n<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>\n<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>\n<p>This is the original DataFrame in the code:<\/p>\n<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\n0 Alice 22\n1 Bob 24 '''<\/pre>\n<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>\n<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')) '''\n0 True\n1 False\nName: User, dtype: bool '''<\/pre>\n<p>The result is a DataFrame with Boolean values that indicate whether a user contains the character <code>'A'<\/code> or not. <\/p>\n<p>Let&#8217;s apply the Tilde operator on the result:<\/p>\n<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')) '''\n0 False\n1 True\nName: User, dtype: bool '''<\/pre>\n<p>Now, we use this DataFrame to access only those rows with users that don&#8217;t contain the character <code>'A'<\/code>. <\/p>\n<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')]\nprint(df) ''' User Age\n1 Bob 24 '''<\/pre>\n<h2>Where to Go From Here?<\/h2>\n<p>Enough theory, let\u2019s get some practice!<\/p>\n<p>To become successful in coding, you need to get out there and solve real problems for real people. That\u2019s how you can become a six-figure earner easily. And that\u2019s how you polish the skills you really need in practice. After all, what\u2019s the use of learning theory that nobody ever needs?<\/p>\n<p><strong>Practice projects is how you sharpen your saw in coding!<\/strong><\/p>\n<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>\n<p>Then become a Python freelance developer! It\u2019s the best way of approaching the task of improving your Python skills\u2014even if you are a complete beginner.<\/p>\n<p>Join my free webinar <a rel=\"noreferrer noopener\" href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\">\u201cHow to Build Your High-Income Skill Python\u201d<\/a> and watch how I grew my coding business online and how you can, too\u2014from the comfort of your own home.<\/p>\n<p><a href=\"https:\/\/blog.finxter.com\/webinar-freelancer\/\" target=\"_blank\" rel=\"noreferrer noopener\">Join the free webinar now!<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python&#8217;s Tilde ~n operator is the bitwise negation operator: it takes the number n as binary number and &#8220;flips&#8221; 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 [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[857],"tags":[73,468,528],"class_list":["post-112998","post","type-post","status-publish","format-standard","hentry","category-python-tut","tag-programming","tag-python","tag-tutorial"],"_links":{"self":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/112998","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/comments?post=112998"}],"version-history":[{"count":0,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/posts\/112998\/revisions"}],"wp:attachment":[{"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/media?parent=112998"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/categories?post=112998"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/sickgaming.net\/blog\/wp-json\/wp\/v2\/tags?post=112998"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}