<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Veeresh Dammur on Medium]]></title>
        <description><![CDATA[Stories by Veeresh Dammur on Medium]]></description>
        <link>https://medium.com/@d.veeresh1?source=rss-cfad6e74e09b------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*x1VZwyc2dBzbi78XPyKgFA.jpeg</url>
            <title>Stories by Veeresh Dammur on Medium</title>
            <link>https://medium.com/@d.veeresh1?source=rss-cfad6e74e09b------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 22 Nov 2026 15:53:52 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@d.veeresh1/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Understanding the division operation of Pandas data frames]]></title>
            <link>https://medium.com/@d.veeresh1/understanding-the-division-operation-of-pandas-data-frames-b4845061dca4?source=rss-cfad6e74e09b------2</link>
            <guid isPermaLink="false">https://medium.com/p/b4845061dca4</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[machine-learning]]></category>
            <category><![CDATA[ai]]></category>
            <category><![CDATA[pandas]]></category>
            <dc:creator><![CDATA[Veeresh Dammur]]></dc:creator>
            <pubDate>Sat, 19 Sep 2020 09:53:43 GMT</pubDate>
            <atom:updated>2020-09-19T09:58:22.247Z</atom:updated>
            <content:encoded><![CDATA[<h3>Understanding the division operation on Pandas data frames</h3><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*fL45tq8hoD5nW9EiqkkQVQ.jpeg" /><figcaption>Photo by <a href="https://unsplash.com/@williambout?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">William Bout</a> on <a href="https://unsplash.com/s/photos/new?utm_source=unsplash&amp;utm_medium=referral&amp;utm_content=creditCopyText">Unsplash</a></figcaption></figure><p><strong>Introduction</strong> :<br>Most of you who are studying or working in the data science field have come across libraries like <strong><em>numpy</em></strong>, <strong><em>pandas</em></strong>. In this post, we will briefly have a look into a division operation of pandas dataframes. <br> <br><strong>what and why Pandas? <br></strong>Pandas is a library in a python programming language which is used for scientific computing and data analysis. It offers tools to perform a wide range of mathematical operations on dataframe (table) data structures. The following statement comes from the official document which sums up its wide popularity in the machine learning library list.</p><blockquote>“fast, flexible, and expressive data structures designed to make working with “relational” or “labeled” data both easy and intuitive.” <a href="https://pandas.pydata.org/">https://pandas.pydata.org/</a></blockquote><p><strong>Arithmetic operations on data frames</strong>: <br>Pandas documentation includes a good explanation about the arithmetic operations that can be performed between two dataframes. In this article, we only look into division operation as the understanding of other operations shouldn’t pose a challenge.</p><p>As we know, we need two operands to perform division operations. so let’s create two simple dataframes.</p><p>Let <em>germany_cars</em> be a dataframe that consist of german car details like <em>car_model</em>, <em>horse_power</em>, and <em>age</em> of the car. <br><strong><em>note</em></strong>: The numbers used in the below dataframes are imaginary values.</p><pre>germany_cars = pd.DataFrame({‘age’: [1.0, 2.0, 3.0],<br>                             ‘horse_power’: [75, 90, 140]},<br>                              index=[‘Benz’, ‘BMW’, ‘Porsche’])</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/243/1*g35zPDTI_IyXb8nYLMzLow.png" /><figcaption>Fig 1: <strong>germany_cars</strong> dataframe</figcaption></figure><p>Let <em>germany_america_cars</em> be a multi-index dataframe where the first index indicates the country name and the second index corresponds to model_name.</p><pre>germany_america_cars = pd.DataFrame(<br>                   {‘age’: [2.0, 4.0,1.5, 1.5, 2.0, 1.5],<br>                    ‘horse_power’: [150, 180, 140, 160, 170, 120]},<br>                    index=[[‘GER’, ‘GER’, ‘GER’, ‘US’, ‘US’, ‘US’],<br>                           [‘Benz’, ‘BMW’, ‘Porsche’, &#39;Ford&#39;,        <br>                            ‘Jeep’, ‘chevrolet’]]<br>                       )</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/301/1*h4ciDc9HEDukXIFV0lCM9g.png" /><figcaption>Fig 2: <strong>germany_america_cars</strong> dataframe</figcaption></figure><p>Now perform the division operation i.e <em>germany_america_cars</em> / <em>germany_cars</em> with setting level parameter as 0.</p><pre>germany_america_cars.div(germany_cars, level = 0)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/314/1*KAcIvEbyB6869FDQ4lGUTQ.png" /><figcaption>Fig 3: Division operation with level = 0 (i.e by <strong>country</strong> name)</figcaption></figure><p><strong>what did just happen here??!!<br></strong>We obtained <em>NaN</em> (Not a Number) in all entries. Because we did not specify the correct level of Index. If you carefully observe the second data frame i.e <em>germany_america_cars</em>, it can be accessed by two indices namely <em>country</em>, <em>model_name</em> of the car. So, Let’s set the value of the level be 1 and perform the division operation by running the below cell.</p><pre>germany_america_cars.div(germany_cars, level = 1)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/318/1*NakxhBk_K7xvTQwBuxhflQ.png" /><figcaption>Fig 4: Division operation with level = 1 (i.e<strong> model_name</strong> of the car)</figcaption></figure><p>From the above table, it is clear that, if there is common index value in both the dataframes, then we can expect a valid answer. For example, the Benz index is present in both data frames, so we obtained 1.0 and 2.0 for the columns age and <em>horse_power</em> after a division operation. If there is no match of the index, for instance, Ford is present in <em>german_america_cars</em> but not in the <em>german_cars</em> dataframe.Then division operation leads to <em>NaN</em> values.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZYQne8IHRulqm06hY7-d4g.png" /><figcaption>Fig 5: Division operation on dataframes under the hood</figcaption></figure><p>The level parameter in the division operation is very helpful when we work with dataframes with multi indices. For instance, if we want to find the gender equality in the salary across different departments of a company.</p><p>If you have any suggestions please write it under the comment section. Until then, Happy learning and reading.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b4845061dca4" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>