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’s quick post below.
Add Pandas index to list
The method pandas.Index.tolist can be used to add a DataFrame index into a Python list.
Here’s a simple example which you can easily copy into your Jupyter Notebook, or other data analysis Python environment.
Let’s start quickly create the students test DataFrame and generate the Index. Run the following code:
#Python3
import pandas as pd
students = pd.DataFrame ([["Donald", 100], ["Harry", 95], ["Liam", None]], columns = ["Name", "GPA"], index = [1,2,3])
std_index = students.index
std_index
The result will be:
Int64Index([1, 2, 3], dtype=’int64′)
Now lets look into the first method to copy the Index into a list:
index_list = list (std_index)
Using index.tolist()
The second method for converting the dataframe index to a Python list is to use the Pandas Index.tolist() method which we mentioned earlier:
index_list = std_index.tolist()
As expected, both methods will return a list, second method should run faster. Try it with the %timeit magic switch.
type(index_list)
# Will return: list
Column index values to a list
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:
students.columns.tolist()
Convert Pandas index to a Numpy Array
If you want to capture the index into a Numpy array you’ll need to use the following Python code
# capturing the index into a Numpy array
index_val = students.index.values
Let’s check for the type of index_val. Run the following:
type(index_val)
# Will return: numpy.ndarray
Questions? Required Improvements? Feel free to leave us a comment.