<?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 Eduardo Calzadilla on Medium]]></title>
        <description><![CDATA[Stories by Eduardo Calzadilla on Medium]]></description>
        <link>https://medium.com/@edalexcalzadilla?source=rss-153b2d0ab7b8------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*oZqkor17yPFjL57vgS8EJA@2x.jpeg</url>
            <title>Stories by Eduardo Calzadilla on Medium</title>
            <link>https://medium.com/@edalexcalzadilla?source=rss-153b2d0ab7b8------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 15 Nov 2026 14:39:18 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@edalexcalzadilla/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[Expanding a list in a column to separate rows]]></title>
            <link>https://medium.com/@edalexcalzadilla/expanding-a-list-in-a-column-to-separate-rows-700d6853e28f?source=rss-153b2d0ab7b8------2</link>
            <guid isPermaLink="false">https://medium.com/p/700d6853e28f</guid>
            <dc:creator><![CDATA[Eduardo Calzadilla]]></dc:creator>
            <pubDate>Tue, 20 Jul 2021 01:30:52 GMT</pubDate>
            <atom:updated>2021-07-20T01:30:52.509Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/607/1*SJTIW7ayl8U-n3xYZIlHMA.png" /></figure><p>For those that are manipulating data using the Pandas package in Python, it might be useful to turn a list of values into separate rows. Having a list of values in a column can be used to represent intersectional points in our data. For example, the different flavors of a donut, fabrics used in clothing and genres of a movie. However, when analyzing this data to find the count, average or other aggregate it is useful to expand the list with a row per element.</p><p>Expanding lists allows us to get a more accurate representation of the data overall. However, this will cause fanout and the overall count and aggregates will be inaccurate. Once this caveat is made known to your audience, then the data will be interpretable.</p><p>For this walkthrough I will be using an IMDB dataset — <strong>title.basics.tsv.gz</strong>. <br>Which you can find <a href="https://www.imdb.com/interfaces/">here</a>, if you want to follow along. In this walkthrough I will be focusing on expanding the genres section for the movies, so that it can be used for tallying up total genre counts.</p><p>I will take you step-by-step as we first get a quick snippet of the table, and how we clean and later transform the data. I am assuming that Pandas is installed already on your device, if not there are other walkthroughs already created for this purpose — including <a href="https://medium.com/bhavaniravi/python-pandas-tutorial-92018da85a33">this post</a>!</p><h4><strong>Preview the data</strong></h4><p>First, let’s take a quick preview into our data. We will do this by using the Pandas built-in <strong>head()</strong> function.</p><pre>df_imdb_title_basics.head(2)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/778/1*7xU2SHerIvGAFEZn8kealQ.png" /></figure><p>Next, we will further look into the data and see if there are any nulls or other anomalies that need to be corrected. Let’s use the built-in <strong>info()</strong> function to detect nulls already in nan form.</p><pre>df_imdb_title_basics.info()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/611/1*adcXCK2ZQO76y2hncQ4kGg.png" /></figure><p>Here we can see that there are 5408 nulls in the genre column. Since this is our area of interest and the nulls account for less than 5% of the original table, then we can safely remove these.</p><h4><strong>Dealing with nulls and other possible null values</strong></h4><p>We will also be looking at missing data that could be expressed as a value other than null. For example, values that could be indicative of missing data include the following:</p><ul><li>Impossible values</li><li>Null categories</li><li>0’s where not logical</li></ul><p>For this we will look at the value counts of all distinct values in each column of the DataFrame (df). The results below are truncated, showing only one column.</p><pre>for column in df_imdb_title_basics.columns:<br>    print(df_imdb_title_basics[column].value_counts())<br>    <br>print(df_imdb_title_basics.info())<br>df_imdb_title_basics.describe()</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/391/1*4982MCYq-dZ_7iFoH1IbRQ.png" /></figure><p>Here we can see the value counts for each genre, that some of these genres are part of a combination of genres and that some are TV shows and not movies. Since, there are only a few odd values present in the dataset, we won’t correct for these, as it doesn’t affect our calculations. We will continue by dropping null values (we will be renaming our working df as well, just in case we would need to recall the original df).</p><pre># Copying df into a new df object<br>df_imdb_title_genre = df_imdb_title_basics.copy()</pre><pre># Removing nulls <br>df_imdb_title_genre.dropna(subset=[&quot;genres&quot;],inplace=True)</pre><pre># Verify new df <br>print(df_imdb_title_genre.info())<br>df_imdb_title_genre.head(2)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/414/1*_ap_LHeYOl8lc0glsuf06A.png" /></figure><p>We can see that the new number of rows is based on the original count of the non-null objects in the genre column. Now we can expand this column to get the count of genres that were part of multiple genre categories.</p><h4>Column value expansion</h4><pre># Converting genre column into a list <br>df_imdb_title_genre[&quot;genres&quot;] =  [&quot;genres&quot;].str.split(&quot;,&quot;)</pre><pre># Again copying into another df to preserve if needed later<br>df_imdb_title_genre_expanded = df_imdb_title_genre.copy()</pre><pre># Expand list into rows<br>rows = [] # Initiate list<br># Use lambda function to create a new row per genre in lists<br>_ = df_imdb_title_genre_expanded.apply(lambda row: [rows.append([row[&#39;tconst&#39;], row[&#39;primary_title&#39;], row[&quot;original_title&quot;], row[&quot;start_year&quot;], row[&quot;runtime_minutes&quot;], genre]) <br>                         for genre in row.genres], axis=1)</pre><pre># Set new df to a DataFrame using the expanded rows df_imdb_title_genre_expanded = pd.DataFrame(rows, columns=df_imdb_title_genre_expanded.columns)</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/850/1*lvafLeNoS_P95bHdDAAhDA.png" /></figure><p>This give us the above, and we can see that the title ‘Sunghursh’ has had its list expanded into rows.</p><p>I hope this short post has been useful in learning how to clean and expand data with values in lists.</p><p>To recap we looked as null values and potential null values, then expanded a list within a column into rows by using the underscore as a variable placeholder and a lambda function to create a new row based on the values found within the list.</p><p>Please take some time to try the above exercises, either using the same dataset or another of your choosing.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=700d6853e28f" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>