Home >Backend Development >Python Tutorial >How to convert Pandas Series to Python list?
Python has become one of the most popular programming languages in recent years, with applications ranging from data analysis to web development to machine learning. One of the key libraries for Python data analysis is Pandas, which provides powerful data structures such as DataFrame and Series to process and manipulate data. In particular, Pandas Series are one-dimensional arrays that can hold any data type, making them a versatile tool for data analysis and manipulation.
In this tutorial, we will explore how to convert a Pandas Series to a Python list. Converting a Series to a List can be useful when we need to pass data to a function or method that only accepts a list as input. We will discuss two ways to convert a Series to a list: using the tolist() function and using the values attribute. After reading this article, we will have a clear understanding of how to convert a Pandas Series to a Python list and why this conversion is useful in data analysis.
Pandas Series is one of the basic data structures provided by the Pandas library. It is a one-dimensional tagged array that can hold any type of data such as integers, floats, and strings. A series can be viewed as a column in a spreadsheet or SQL table.
On the other hand, a Python list is a built-in data structure that can hold any type of data. It is an ordered collection of objects, and each object can be accessed using its index. Lists are mutable, which means we can add, remove and modify elements in the list.
In this part of the article, we will discuss two methods of converting a Pandas Series to a Python list. The first method involves using the tolist() function, while the second method uses the values attribute.
tolist() method is a built-in function of Pandas Series that helps in converting Series to Python list. Its purpose is to convert the values of a series into list format while maintaining their original order. In other words, the tolist() method generates a new list containing the same values as the original Series, in the same order as they appear in the Series.
The following is a sample code snippet demonstrating how to use the tolist() function:
# importing pandas library and aliasing it as 'pd' import pandas as pd # Create a Pandas Series data = pd.Series([1, 2, 3, 4, 5]) # Convert the Series to a list using the tolist() function list_data = data.tolist() # Print the list print(list_data)
The above code imports the Pandas library and creates a new Pandas Series named "data" that contains a sequence of five integers. Integers are enclosed in square brackets and separated by commas. The sequence starts with 1 and ends with 5.
Next, the code uses the tolist() method to convert the "data" Series into Python list format. This function call generates a new list object named "list_data" containing the same values as the original "data" series, in the same order.
Finally, the code prints the newly generated list using a print() statement. The output of this code snippet displays the contents of the "list_data" object as a list of integers, that is, [1, 2, 3, 4, 5].
[1, 2, 3, 4, 5]
The output generated by the above code snippet clearly shows that the Pandas Series object was successfully converted to Python list format.
Another way to convert a Pandas Series to a Python list is to take advantage of the "values" attribute, which is another built-in feature of Pandas. This property helps retrieve data from a Pandas Series as a NumPy array. Once the data has been retrieved as an array, it can be easily converted to a Python list using the built-in "tolist()" method.
Here is a sample code snippet demonstrating how to use these values:
# importing pandas library and aliasing it as 'pd' import pandas as pd # Create a Pandas Series data = pd.Series([1, 2, 3, 4, 5]) # Get the values of the Series as a NumPy array and convert it to a list list_data = data.values.tolist() # Print the list print(list_data)
In the above code, we first import the Pandas library and create a Pandas Series object named "data" containing the values [1, 2, 3, 4, 5].
Next, we extract the values of the Series and convert them into NumPy arrays using the ".values" property. The ".tolist()" method is then applied to the NumPy array, producing a new list containing the same values as the original Series, in the same order. This new list is assigned to the variable "list_data".
Finally, the code prints the contents of the "list_data" variable using the "print()" function, which displays the list [1, 2, 3, 4, 5] on the console.
[1, 2, 3, 4, 5]
From the output we can see that the values attribute successfully converts the Pandas Series to a Python list.
In this article, we discussed two ways to convert a Pandas Series to a Python list: using the tolist() function and using the value attribute. We explained that converting a series to a list is useful in situations where you need to pass data to a function or method that only accepts a list as input. We provide an example for each method, explaining how they work and the output we should expect. Both methods are easy to use and can be applied to different scenarios depending on your data analysis needs. By following the examples in this tutorial, you should now be able to easily convert a Pandas Series to a Python list.
The above is the detailed content of How to convert Pandas Series to Python list?. For more information, please follow other related articles on the PHP Chinese website!