<?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 Gota Shirato on Medium]]></title>
        <description><![CDATA[Stories by Gota Shirato on Medium]]></description>
        <link>https://medium.com/@gotashirato?source=rss-24fe39a0c3b1------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*ht1mmkE8583W2k3U.</url>
            <title>Stories by Gota Shirato on Medium</title>
            <link>https://medium.com/@gotashirato?source=rss-24fe39a0c3b1------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Thu, 19 Nov 2026 05:48:26 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@gotashirato/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[How to import CSV file with multi-level columns (Python Basics) and a question]]></title>
            <link>https://medium.com/@gotashirato/how-to-import-csv-file-with-multi-level-columns-python-basics-and-a-question-b67cbbbc174d?source=rss-24fe39a0c3b1------2</link>
            <guid isPermaLink="false">https://medium.com/p/b67cbbbc174d</guid>
            <category><![CDATA[data-science]]></category>
            <category><![CDATA[pandas]]></category>
            <category><![CDATA[python]]></category>
            <dc:creator><![CDATA[Gota Shirato]]></dc:creator>
            <pubDate>Thu, 04 Apr 2019 14:23:17 GMT</pubDate>
            <atom:updated>2019-04-04T23:17:05.438Z</atom:updated>
            <content:encoded><![CDATA[<h3>How to import CSV file with multi-level columns (Python Basics)</h3><p>Now the <a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.Panel.html">pandas panel</a> is deprecated and they recommend to use MultiIndex instead, you may be gonna have to work on a CSV file with multi-level columns to use a 3D DataFrame.(To be honest, I never used the pandas Panel though. It is still important to know how to treat 3D data well.)</p><p><a href="https://pandas.pydata.org/pandas-docs/stable/user_guide/advanced.html">MultiIndex / advanced indexing - pandas 1.2.3 documentation</a></p><p>Struggling with an import of one?</p><p>You can find the same kind of issue <a href="https://stackoverflow.com/questions/21318865/read-multi-index-on-the-columns-from-csv-file">here</a> or in the link below.</p><p><a href="https://stackoverflow.com/questions/21318865/read-multi-index-on-the-columns-from-csv-file">Read multi-index on the columns from csv file</a></p><p>Let’s work on a CSV file that looks like this.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/45284372394ab67c4ccbbd7a7975cbf4/href">https://medium.com/media/45284372394ab67c4ccbbd7a7975cbf4/href</a></iframe><p>It has as columns “Name”, “Language”, “Height” and “Weight” for 2018 and 2019.</p><p>This may not be the best solution, please comment if you have a better one.</p><p>Now let’s read this CSV file in the simplest way using pandas.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/12ad8b4d46f0740ac87aded3b0f1f3fe/href">https://medium.com/media/12ad8b4d46f0740ac87aded3b0f1f3fe/href</a></iframe><p>And it does not seem quite alright (Of course).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/740/1*K98oab3QPslP1U3ehTKzpg.png" /><figcaption>import_csv.py</figcaption></figure><p>So we set the first 2 rows as the header because this CSV has multi-level columns. And we want the “Name” column to be the index. (Suppose it works as ID)</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/e8a60a981847554ae8a6750dfaca6c0e/href">https://medium.com/media/e8a60a981847554ae8a6750dfaca6c0e/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*1WlvweFkGIGuc8oP6OCV_Q.png" /><figcaption>read_csv_more_effectively.py</figcaption></figure><p>It looks better except it has “Unnamed” columns. It is OK to have columns without name per se. But as we can see from the result, data in 2018 does not include “weight” columns. It is quite a problem.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/180/1*ngugXIAbO7gBuLl1ayRCPQ.png" /><figcaption>df.loc[:, “2018”] has no weight column</figcaption></figure><p>How we solve this? Here, we use <em>pd.MultiIndex.from_tuples() </em>to create new column-names rows. In a straightforward way, you can write column names one by one. If a column name has multiple levels, put all the names of the column in a tuple, like (“level1”, “level”2, “level3”). <br>If it has only one level, write something like (“column name”, ).</p><p>In this case, level1 can be one of 3, “2018”, “2019”, “lang” and level2 can be “height” or “weight”.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/11bc50c792262f3092bee1a8d715556b/href">https://medium.com/media/11bc50c792262f3092bee1a8d715556b/href</a></iframe><figure><img alt="" src="https://cdn-images-1.medium.com/max/560/1*HH3iY8xjwwp2Ro6dyKNRvw.png" /><figcaption>Voila.</figcaption></figure><figure><img alt="" src="https://cdn-images-1.medium.com/max/290/1*us5_n8A1E4ZTiXyf8BwuaA.png" /><figcaption>df.loc[:, “2018”]</figcaption></figure><p>If NaN bothers you, you can use (“lang”, “”) instead of (“lang”, ). (TBC)</p><p>The MultiIndex function can be useful if you wanna use time-series data. But there may be another way to do, if so please tell me :)</p><p>About the author</p><p><a href="https://twitter.com/holokeum?lang=en">stats-mec🇯🇵🇫🇷 (@holokeum) | Twitter</a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b67cbbbc174d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>