<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mac McCarthy</title>
    <description>The latest articles on DEV Community by Mac McCarthy (@mcshakes).</description>
    <link>https://dev.to/mcshakes</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F72948%2Fc6949dbc-2457-4ff8-b32d-d5ecf5499c51.jpeg</url>
      <title>DEV Community: Mac McCarthy</title>
      <link>https://dev.to/mcshakes</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mcshakes"/>
    <language>en</language>
    <item>
      <title>Pandas and Beer</title>
      <dc:creator>Mac McCarthy</dc:creator>
      <pubDate>Mon, 12 Oct 2020 19:36:47 +0000</pubDate>
      <link>https://dev.to/mcshakes/pandas-and-beer-5fa6</link>
      <guid>https://dev.to/mcshakes/pandas-and-beer-5fa6</guid>
      <description>&lt;p&gt;For the first time since its inception in 1982, the Great American Beer Festival has been canceled, in turn, replaced by an opportunity for a lonely pub crawl. Digital passports were handed out, and "attendees" got their first looks at the available breweries in their area:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb4cr8hq0tmyxfm15eyk3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb4cr8hq0tmyxfm15eyk3.png" alt="Alt Text" width="800" height="716"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, that view is all well and good. Provides the needed information, along with directions or a website a user could peruse to find beers that can quench their unshakeable thirst during this pandemic. &lt;/p&gt;

&lt;p&gt;What is it missing? Python. And Pandas.&lt;/p&gt;

&lt;p&gt;After scraping (may write a future post) the data into a CSV, I had a collection separated by three columns: Brewery name, Location (city/state), and Deal.&lt;/p&gt;

&lt;p&gt;As always, the first step is to import all of that and turn it into a DataFrame:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import pandas as pd
df = pd.read_csv("breweries.csv")

pd.set_option("max_colwidth", None)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;set_option&lt;/code&gt; was an important addition. Otherwise, crucial information would be cut off in the limited column width.&lt;/p&gt;

&lt;h3&gt;
  
  
  1) Find all breweries in a city
&lt;/h3&gt;

&lt;p&gt;This required a simple column lookup via location:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;denver_breweries = df.loc[df["Location"] == "Denver, Colorado"]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This got me a better view of the possibilities around me:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvs953j6qmu7x30a3ubhb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fvs953j6qmu7x30a3ubhb.png" alt="Alt Text" width="800" height="325"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  2) Find breweries according to deals
&lt;/h3&gt;

&lt;p&gt;Now, what if I wasn't alone on my boozy adventures? What if I actually had friends that wanted to see me? This is when a Buy One - Get One Free deal would come in handy. I could search by substrings within the &lt;code&gt;Deal&lt;/code&gt; column.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;def find_bogo_deals(brew_list):    
    new_df = brew_list[brew_list['Deal'].str.contains("BOGO") | brew_list['Deal'].str.contains("buy one, get one", case=False)]
    df = pd.DataFrame(new_df)
    return df

find_bogo_deals(denver_breweries)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;I declared the variable denver_breweries at the beginning of the notebook&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Now, we are cooking with fire!&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb2c9q2ihe7sj5ix1n5le.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fb2c9q2ihe7sj5ix1n5le.png" alt="Alt Text" width="800" height="329"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;But what if we wanted the good stuff? Those special releases that have been brewed and barrel-aged since last year, in anticipation for the fall of 2020. Then we would just change up our substring filter using regex.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import re

def find_specials(brew_list):

    special_list = ["special", "exclusive", "special release"]

    esc_lst = [re.escape(s) for s in special_list]   

    spec_pattern = '|'.join(esc_lst)

    new_df = brew_list[brew_list['Deal'].str.contains(spec_pattern, case=False)]    

    df = pd.DataFrame(new_df)

    return df


find_specials(denver_breweries)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcvxjowywp0qooeeg8s5o.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fi%2Fcvxjowywp0qooeeg8s5o.png" alt="Alt Text" width="800" height="369"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Excellent! This list should ensure that I won't be driving.&lt;/p&gt;

&lt;p&gt;Obviously, we could do a lot more manipulation here: splitting up location to be City - State; cleaning up the text in the &lt;code&gt;Deal&lt;/code&gt; column to remove newlines; even exporting that CSV into a SQL database to play around some more.&lt;/p&gt;

&lt;p&gt;In any case, I hope this post inspired you to break out your notebooks and apply Python &amp;amp; Pandas to your everyday life. GABF deals end October 17th (this Saturday), so don your mask and get out there to support your local breweries in these trying times.&lt;/p&gt;

</description>
      <category>python</category>
      <category>pandas</category>
      <category>beer</category>
      <category>data</category>
    </item>
  </channel>
</rss>
