<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Python Basics | Data for Everybody</title>
	<atom:link href="https://www.dataforeverybody.com/category/python/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dataforeverybody.com</link>
	<description>Beginning Data Analysis and Visualization</description>
	<lastBuildDate>Sun, 28 Nov 2021 16:09:06 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.9.4</generator>

<image>
	<url>https://www.dataforeverybody.com/wp-content/uploads/2026/03/cropped-logo-512-32x32.png</url>
	<title>Python Basics | Data for Everybody</title>
	<link>https://www.dataforeverybody.com</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>How to convert a Pandas DataFrame index to a Python list?</title>
		<link>https://www.dataforeverybody.com/convert-pandas-index-list-array/</link>
					<comments>https://www.dataforeverybody.com/convert-pandas-index-list-array/#respond</comments>
		
		<dc:creator><![CDATA[Gili]]></dc:creator>
		<pubDate>Sun, 04 Oct 2020 15:24:23 +0000</pubDate>
				<category><![CDATA[Python Basics]]></category>
		<guid isPermaLink="false">https://www.dataforeverybody.com/?p=155</guid>

					<description><![CDATA[<p>The other day i was researching for a way to capture the index of a DataFrame i created out of a CSV file into a ... </p>
<p class="read-more-container"><a title="How to convert a Pandas DataFrame index to a Python list?" class="read-more button" href="https://www.dataforeverybody.com/convert-pandas-index-list-array/#more-155" aria-label="More on How to convert a Pandas DataFrame index to a Python list?">Read more</a></p>
<p>The post <a href="https://www.dataforeverybody.com/convert-pandas-index-list-array/">How to convert a Pandas DataFrame index to a Python list?</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></description>
										<content:encoded><![CDATA[<p>The other day i was researching for a way to capture the index of a DataFrame i created out of a CSV file into a simple Python list for further manipulation. Apparently there are a couple of simple ways to do that, and i would like to share in today&#8217;s quick post below.</p>



<h2 class="wp-block-heading">Add Pandas index to list </h2>



<p>The method <strong>pandas.Index.tolist </strong>can be used to <strong>add a DataFrame index into a Python list</strong>. </p>



<p>Here&#8217;s a simple example which you can easily copy into your Jupyter Notebook, or other data analysis Python environment.</p>



<p>Let&#8217;s start quickly create the students test DataFrame and generate the Index. Run the following code:</p>



<pre class="wp-block-code"><code>#Python3
import pandas as pd
students = pd.DataFrame (&#91;&#91;"Donald", 100], &#91;"Harry", 95], &#91;"Liam", None]], columns = &#91;"Name", "GPA"], index = &#91;1,2,3])

std_index = students.index

std_index</code></pre>



<p>The result will be:</p>



<p>Int64Index([1, 2, 3], dtype=&#8217;int64&#8242;)</p>



<p>Now lets look into the first method to copy the Index into a list:</p>



<pre class="wp-block-code"><code>index_list = list (std_index)</code></pre>



<h3 class="wp-block-heading">Using index.tolist()</h3>



<p>The second method for converting the dataframe index to a Python list is to use the Pandas Index.tolist() method which we mentioned earlier:</p>



<pre class="wp-block-code"><code>index_list = std_index.tolist()</code></pre>



<p>As expected, both methods will return a list, second method should run faster. Try it with the %timeit magic switch.</p>



<pre class="wp-block-code"><code>type(index_list) 

# Will return: list</code></pre>



<h3 class="wp-block-heading">Column index values to a list </h3>



<p>Tip: You can also use the tolist() method to capture not only the row index but also the column index as well. df.columns.tolist() will do the trick, as shown below:</p>



<pre class="wp-block-code"><code>students.columns.tolist()</code></pre>



<h2 class="wp-block-heading">Convert Pandas index to a Numpy Array</h2>



<p>If you want to capture the index into a Numpy array you&#8217;ll need to use the following Python code</p>



<pre class="wp-block-code"><code># capturing the index into a Numpy array
index_val = students.index.values</code></pre>



<p>Let&#8217;s check for the type of index_val. Run the following:</p>



<pre class="wp-block-code"><code>type(index_val)

# Will return: numpy.ndarray</code></pre>



<p>Questions? Required Improvements? Feel free to leave us a comment.</p><p>The post <a href="https://www.dataforeverybody.com/convert-pandas-index-list-array/">How to convert a Pandas DataFrame index to a Python list?</a> first appeared on <a href="https://www.dataforeverybody.com">Data for Everybody</a>.</p>]]></content:encoded>
					
					<wfw:commentRss>https://www.dataforeverybody.com/convert-pandas-index-list-array/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>

<!--
Performance optimized by W3 Total Cache. Learn more: https://www.boldgrid.com/w3-total-cache/?utm_source=w3tc&utm_medium=footer_comment&utm_campaign=free_plugin

Page Caching using Disk: Enhanced 

Served from: www.dataforeverybody.com @ 2026-05-01 02:00:51 by W3 Total Cache
-->