How to convert Pandas Series to Python list?

PHPz
Release: 2023-09-14 16:13:02
forward
761 people have browsed it

如何将Pandas Series转换为Python列表?

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.

How to convert a Pandas series to a Python list?

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.

Method 1: Use tolist() function

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.

Example

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)
Copy after login

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].

Output

[1, 2, 3, 4, 5]
Copy after login
Copy after login

The output generated by the above code snippet clearly shows that the Pandas Series object was successfully converted to Python list format.

Method 2: Use the values ​​attribute

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)
Copy after login

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.

Output

[1, 2, 3, 4, 5]
Copy after login
Copy after login

From the output we can see that the values ​​attribute successfully converts the Pandas Series to a Python list.

in conclusion

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template