- Trending Categories
- Data Structure
- Operating System
- MS Excel
- C Programming
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to Convert a 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 data analysis in Python is Pandas, which provides powerful data structures like DataFrames and Series for handling and manipulating data. In particular, Pandas Series are one-dimensional arrays that can hold any data type, making them versatile tools 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 in situations where we need to pass data to functions or methods that only accept lists as inputs. We will discuss two methods for converting a Series to a list: using the tolist() function and using the values attribute. By the end of this article, we will have a clear understanding of how to convert a Pandas Series to a Python list and why this conversion can be useful in data analysis.
How to Convert a Pandas Series to a Python list?
Pandas Series is one of the fundamental data structures provided by the Pandas library. It is a one-dimensional labeled array that can hold data of any type, such as integers, floats, and strings. A Series can be thought of as a column in a spreadsheet or a SQL table.
On the other hand, a Python list is a built-in data structure that can hold data of any type. It is an ordered collection of objects, and each object can be accessed using its index. Lists are mutable, which means that we can add, remove, and modify the elements in a list.
In this section of the article, we will discuss two methods for 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: Using the tolist() function
The tolist() method is built-in functionality of Pandas Series that facilitates the conversion of a Series into a Python list. It serves the purpose of transforming the values of a Series into a list format while maintaining their original order. In other words, the tolist() method generates a new list that contains the same values as the original Series, in the same sequence as they appeared in the Series.
Here's an example code snippet that demonstrates 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 called "data" that contains a sequence of five integers. The 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 a Python list format. This function call generates a new list object called "list_data" that contains the same values as the original "data" Series in the same order.
Finally, the code prints the newly generated list using the print() statement. The output of this code snippet will display the contents of the "list_data" object as a list of integers, which will be [1, 2, 3, 4, 5].
[1, 2, 3, 4, 5]
The output produced by the above code snippet clearly demonstrates the successful conversion of the Pandas Series object into a Python list format.
Method 2: Using the values attribute
An alternative approach to transforming a Pandas Series to a Python list is by utilizing the "values" attribute, which is another built-in functionality of Pandas. This attribute facilitates the retrieval of data from a Pandas Series in the form of a NumPy array. Once the data is retrieved in the form of an array, it can be easily transformed into a Python list, using the built-in "tolist()" method.
Here's an example code snippet that demonstrates how to use the 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 called "data" containing the values [1, 2, 3, 4, 5].
Next, we extract the values of the Series and converts them into a NumPy array using the ".values" attribute. Then, the ".tolist()" method is applied to the NumPy array, which generates 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]
As we can see from the output, the values attribute successfully converted the Pandas Series to a Python list.
In this article, we discussed two methods for converting a Pandas Series to a Python list: using the tolist() function and using the values attribute. We explained that converting a Series to a list can be useful in situations where we need to pass data to functions or methods that only accept lists as inputs. We provided an example for each of the methods, explaining how they work and what output we should expect. Both methods are easy to use and can be applied in different scenarios depending on your data analysis requirements. By following the examples in this tutorial, you should now be able to easily convert a Pandas Series to a Python list.
Kickstart Your Career
Get certified by completing the courseGet Started