Convert DataFrame to a List
DataFrames or Series are very useful data structures, but not all Python libraries use these types. So, you need to convert them to other Python types, such as a list. In previous lessons, we learned how to create Series and DataFrames from a Python list. Here, we see how to convert them back.
We'll cover the following
Convert data to list without index
Both Series and DataFrame have a very simple function, tolist()
. From the name you can immediately tell what the function does.
You can call the function directly from a Series or from the attribute values. They are the same.
Notice:
- The
DataFrame
only supports the second method, which is called from the attributevalues
.- The type of the result from DataFrame is a nested list.
- The values attribute does not include labels like row or column names, so, you wouldn’t get the index. If you want to keep the labels, just see the next section.
Get hands-on with 1200+ tech skills courses.