<?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 Ankit Gandhi on Medium]]></title>
        <description><![CDATA[Stories by Ankit Gandhi on Medium]]></description>
        <link>https://medium.com/@ank0409?source=rss-314f0ba75996------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*TAAhShQ5f3MO-K-vf5vEUA@2x.jpeg</url>
            <title>Stories by Ankit Gandhi on Medium</title>
            <link>https://medium.com/@ank0409?source=rss-314f0ba75996------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 29 Nov 2026 02:30:02 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@ank0409/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[Replacing Excel with Python]]></title>
            <link>https://medium.com/data-science/replacing-excel-with-python-30aa060d35e?source=rss-314f0ba75996------2</link>
            <guid isPermaLink="false">https://medium.com/p/30aa060d35e</guid>
            <category><![CDATA[excel]]></category>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[automation]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Ankit Gandhi]]></dc:creator>
            <pubDate>Tue, 05 Mar 2019 06:31:08 GMT</pubDate>
            <atom:updated>2019-03-06T13:46:58.246Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/0*mmwdt7ZWoEVHv2g5" /><figcaption>Photo by <a href="https://unsplash.com/@rawpixel?utm_source=medium&amp;utm_medium=referral">rawpixel</a> on <a href="https://unsplash.com/?utm_source=medium&amp;utm_medium=referral">Unsplash</a></figcaption></figure><p>After spending almost a decade with my first love Excel, its time to move on and search for a better half who in thick and thin of my daily tasks is with me and is much better and faster and who can give me a cutting edge in the challenging technological times where new technology is getting ditched by something new at a very rapid pace. The idea is to replicate almost all excel functionalities in Python, be it using a simple filter or a complex task of creating an array of data from the rows and crunching them to get fancy results</p><p>The approach followed here is to start from simple tasks and move to complex computational tasks. I will encourage you to please replicate the steps yourself for your better understanding.</p><p>The inspiration to create something like this came from the non-availability of a free tutorial which literally gives all. I heavily read and follow Python documentation and you will find a lot of inspiration from that site.</p><p><em>GitHub repo link</em></p><p><a href="https://github.com/ank0409/Ditching-Excel-for-Python">https://github.com/ank0409/Ditching-Excel-for-Python</a></p><h4>Importing Excel Files into a Pandas DataFrame</h4><p>Initial step is to import excel files into DataFrame so we can perform all our tasks on it. I will be demonstrating the <strong>read_excel </strong>method of Pandas which supports <strong>xls</strong> and <strong>xlsx</strong> file extensions. <br><strong>read_csv</strong> is same as using <strong>read_excel</strong>, we wont go in depth but I will share an example.</p><p>Though <strong>read_excel</strong> method includes million arguments but I will make you familiarise with the most common ones that will come very handy in day to day operations.</p><p>Though read_excel method includes million arguments but I will make you familiarise with the most common ones that will come very handy in day to day operations<br>I’ll be using the Iris sample dataset which is freely available online for educational purpose.</p><p>Please follow the below link to download the dataset and ensure to save it in the same folder where you are saving your python file</p><p><a href="https://archive.ics.uci.edu/ml/datasets/iris">https://archive.ics.uci.edu/ml/datasets/iris</a></p><h4><strong>The first step is to import necessary libraries in Python</strong></h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*-XcLJONZJIkg8Bb1UbsX0A.png" /></figure><p>We can import the spreadsheet data into Python using the following code:</p><p>pandas.read_excel(io, sheet_name=0, header=0, names=None, index_col=None, parse_cols=None, usecols=None, squeeze=False, dtype=None, engine=None, converters=None, true_values=None, false_values=None, skiprows=None, nrows=None, na_values=None, keep_default_na=True, verbose=False, parse_dates=False, date_parser=None, thousands=None, comment=None, skip_footer=0, skipfooter=0, convert_float=True, mangle_dupe_cols=True, **kwds)</p><p>Since there’s a plethora of arguments available, lets look at the most used one’s.</p><h4>Important Pandas read_excel options</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/832/1*VY3YEcbwvUgqYhQeS-Z7Tw.png" /></figure><p>If we are using the path for our local file by default its separated by “\” however python accepts “/”, so make to change the slashes or simply add the file in the same folder where your python file is. Should you require detailed explanation on the above, refer to the below medium article. <a href="https://medium.com/@ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f">https://medium.com/@ageitgey/python-3-quick-tip-the-easy-way-to-deal-with-file-paths-on-windows-mac-and-linux-11a072b58d5f</a></p><p>We can use Python to scan files in a directory and pick out the ones we want.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/835/1*RHObn_4cp-PYlcb4sbKPzg.png" /></figure><h4>Import a specific sheet</h4><p>By default, the first sheet in the file is imported to the dataframe as it is. <br>Using the sheet_name argument we can explicitly mention the sheet that we want to import. Default value is 0 i.e. the first sheet in the file. <br>We can either mention the name of the sheet(s) or pass an integer value to refer to the index of the sheet</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*kLK2XGk98uyiEaG1IFhhQA.png" /></figure><h4>Using a column from the sheet as an Index</h4><p>Unless explicitly mentioned, an index column is added to the DataFrame which by default starts from a 0. <br>Using the index_col argument we can manipulate the index column in our dataframe, if we set the value 0 from none, it will use the first column as our index.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/843/1*7eZlLWayPZhgfSaAYCH-qQ.png" /></figure><h4>Skip rows and columns</h4><p>The default read_excel parameters assumes that the first row is a list of column names, which is incorporated automatically as column labels within the DataFrame. <br>Using the arguments like skiprows and header we can manipulate the behavior of the imported DataFrame.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*xQX3IfqCvMcBUa8ramh2eQ.png" /></figure><h4>Import specific column(s)</h4><p>Using the usecols argument we can specify if we have import a specific column in our DataFrame</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/847/1*R-iovQtpCGGDziDzmzd2lA.png" /></figure><p><em>Its not the end of the features available however its a start and you can play around with them as per your requirements</em></p><h3>Lets have a look at the data from 10,000 feet</h3><p>As now we have our DataFrame, lets look at the data from multiple angles just to get a hang of it/ <br>Pandas have plenty of functions available that we can use. We’ll use some of them to have a glimpse of our dataset.</p><h4>“Head” to “Tail”:</h4><p>To view the first or last <strong>five</strong> rows. <br><em>Default is five, however the argument allows us to use a specific number</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*8COn1kvfNA4BaRgUzrvoZA.png" /></figure><h4>View specific column’s data</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/843/1*UhrTYStrB7s8-yn38huxeA.png" /></figure><h4>Getting the name of all columns</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*Lpf00AQl5-LSrf-9R4mIzQ.png" /></figure><h4>Info Method</h4><p>Gives the summary of DataFrame</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*obVK7TT3Gr_4K7vV2Fel8g.png" /></figure><h4>Shape Method</h4><p>Returns the dimensions of DataFrame</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*PAQp6q89wBWcArhF4z_EcQ.png" /></figure><h4>Look at the datatypes in DataFrame</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*o80Ud2XBIYx6qUHX5J2j2A.png" /></figure><h3>Slice and Dice i.e. Excel filters</h3><p>Descriptive reporting is all about data subsets and aggregations, the moment we are to understand our data a little bit we start using filters to look at the smaller sets of data or view a particular column maybe to have a better understanding. <br>Python offers a lot of different methods to slice and dice the DataFrames, we’ll play around with a couple of them to have an understanding of how it works</p><h4>View a specific column</h4><p>There exists three main methods to select columns:</p><ul><li>Use dot notation: e.g. data.column_name</li><li>Use square braces and the name of the column:, e.g. data[‘column_name’]</li><li>Use numeric indexing and the iloc selector data.loc[:, ‘column_number’]</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/842/1*y9erh3u9h5pydCHwV-WiGQ.png" /></figure><h4>View multiple columns</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/851/1*MMnvcxDIM8LBETWI2d6G0Q.png" /></figure><h4>View specific row’s data</h4><p>The method used here is slicing using the loc function, where we can specify the start and end row separated by colon <br>Remember, <strong>index starts from a 0 and not 1</strong></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/839/1*IHP6Uyw5Zaz5_rn02qrBbQ.png" /></figure><h4>Slice rows and columns together</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/841/1*ZJ_-Dr5TXMuw4ardU53NlA.png" /></figure><h4>Filter data in a column</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/847/1*-GgjcOEOKFg-ySIai3fhfw.png" /></figure><h4>Filter multiple values</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/843/1*YOUUKGeX9Yw_Rs7XUw6-mg.png" /></figure><h4>Filter multiple values using a list</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/843/1*-Mfuw5U9CC43kPogYbhs-Q.png" /></figure><h4>Filter values NOT in list or not equal to in Excel</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*ajBgjixFOD8Rr4k4my9j9A.png" /></figure><h4>Filter using using multiple conditions in multiple columns</h4><p><strong>The input should always be a list</strong> <br>We can use this method to replicate advanced filter function in excel</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/851/1*2E5am09wc1R9CJpOcnk3wQ.png" /></figure><h4>Filter using numeric conditions</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*gQf1H3HIrwoLy0DShzvZ1Q.png" /></figure><h4>Replicate the custom filter in Excel</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*hop3t-vKtJLVpUg5nUxpNA.png" /></figure><h4>Combine two filters to get the result</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*-E0KMtcUiXOdDbVwecUNmA.png" /></figure><h4>Contains function in Excel</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/845/1*dSYH_b5cFX-UbJUYQpTr7g.png" /></figure><h4>Get the unique values from DataFrame</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*u2LYriSe143rb0Gw28-r2g.png" /></figure><p><em>If we want to view the entire DataFrame with the unique values, we can use the drop_duplicates method</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*_U_WqADsk0ByBSHr9WgyOg.png" /></figure><h4>Sort Values</h4><p>Sort data by a certain column, by default the sorting is ascending</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Ct_dyxEwFAOIN0aeHeXiIQ.png" /></figure><h3>Statistical summary of data</h3><h4>DataFrame Describe method</h4><p><em>Generate descriptive statistics that summarize the central tendency, dispersion and shape of a dataset’s distribution, excluding NaN values</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1TTvzcBFMIK873z3BuYfNw.png" /></figure><p>Summary stats of character columns</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*EFqPlgdA21dpPzpTrhafqA.png" /></figure><h3>Data Aggregation</h3><h4>Counting the unique values of a particular column</h4><p><em>Resulting output is a Series. You can refer it as a Single column Pivot Table</em></p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*jntTFcQNTrKdyNTlEPmCHQ.png" /></figure><h4>Count cells</h4><p>Count non-NA cells for each column or row</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*3QOD9WMP46r2QhaKvfi86Q.png" /></figure><h4>Sum</h4><p>Summarising the data to get a snapshot of either by rows or columns</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*bIQBHNyp1DmNhvjWR1PRLA.png" /></figure><p>Replicates the method of adding a total column against each row</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*VW3l1NotlzEcwxHaN8x6Rw.png" /></figure><h4>Add a total column to the existing dataset</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZK8WRpzr8aCB6vqT4wrytA.png" /></figure><h4>Sum of specific columns, use the loc methods and pass the column names</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xgCl5sGMvbXQKyQst2nSOA.png" /></figure><h4>Or, we can use the below method</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*R1DrP2_-hOuNhOB7d_SBRw.png" /></figure><h4>Don’t like the new column, delete it using drop method</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*SgYcO7sbvYlUZw_1VhR0KQ.png" /></figure><h4>Adding sum-total beneath each column</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*mT6bzKDu-J6s7VlNNyT6Ng.png" /></figure><p>A lot has been done above, the approach that we are using is:</p><ul><li>Sum_Total: Do the sum of columns</li><li>T_Sum: Convert the series output to DataFrame and transpose</li><li>Re-index to add missing columns</li><li>Row_Total: append T_Sum to existing DataFrame</li></ul><h4>Sum based on criteria i.e. Sumif in Excel</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*oH4cS5qBCCQlVsIV-5B7-w.png" /></figure><h4>Sumifs</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*B69D26QN4LyjbHpH5Y38VA.png" /></figure><h4>Averageif</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iqlHjPpabn9OvBX6lEZMfQ.png" /></figure><h4>Max</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-UBdP5-oRbR993VK12fSHw.png" /></figure><h4>Min</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NkZd22rozp0tfJRiHHGi_w.png" /></figure><h4>Groupby i.e. Subtotals in Excel</h4><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p7cOtWSzYZ0IipYZtadWPA.png" /></figure><h3>Pivot Tables in DataFrames i.e. Pivot Tables in Excel</h3><p>Who doesn’t love a Pivot Table in Excel, its one the best ways to analyse your data, have a quick overview of the information, helps you slice and dice the data with a super easy interface, helps you plots graphs basis on the data, add calculative columns etc. <br>No, we wont have an interface to work, we’ll have to explicitly write the code to get the output, No, it wont generate charts for you, but I don’t think we can complete a tutorial without learning about the Pivot tables.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*c5lofdpDHLLirfJ64XgJ-g.png" /></figure><p>A simple Pivot table showing us the sum of SepalWidth in values, SepalLength in Row Column and Name in Column Labels</p><p>Lets see if we can complicate it a bit.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Cuw77zZZg_OAyAI9MTeBCw.png" /></figure><p>Blanks are now replaced with 0’s by using the fill_value argument</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*YiHuGTQ0qcabkXRN2DmuOw.png" /></figure><p>We can have individual calculations on values using dictionary method and can also have multiple calculations on values</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*Bux_bVBoq-P7mUESQx93bQ.png" /></figure><p>If we use margins argument, we can have total row added</p><h3>Vlookup</h3><p>What a magical formula is vlookup in Excel, I think its the first thing that everyone wants to learn before learning how to even add. Looks fascinating when someone is applying vlookup, looks like magic when we get the output. Makes life easy. I can with very much confidence can say its the backbone of every data wrangling action performed on the spreadsheet.</p><p><strong>Unfortunately</strong> we don’t have a vlookup function in Pandas!</p><p>Since we don’t have a “Vlookup” function in Pandas, Merge is used as an alternate which is same as SQL. There are a total of four merge options available:</p><ul><li>‘left’ — Use the shared column from the left DataFrame and match to right DataFrame. Fill in any N/A as NaN</li><li>‘right’ — Use the shared column from the right DataFrame and match to left DataFrame. Fill in any N/A as NaN</li><li>‘inner’ — Only show data where the two shared columns overlap. Default method.</li><li>‘outer’ — Return all records when there is a match in either left or right DataFrame.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qbez8z0XS9dWPCUDFqs2Bg.png" /></figure><p>The above might not be the best example to support the concept, however the working is the same.</p><p>As they say, “No perfect tutorial exists”, nor does mine :)</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=30aa060d35e" width="1" height="1" alt=""><hr><p><a href="https://medium.com/data-science/replacing-excel-with-python-30aa060d35e">Replacing Excel with Python</a> was originally published in <a href="https://medium.com/data-science">TDS Archive</a> on Medium, where people are continuing the conversation by highlighting and responding to this story.</p>]]></content:encoded>
        </item>
    </channel>
</rss>