Python Pandas - Converting Series to Other Objects



Pandas Series is a one-dimensional array-like object containing data of any type, such as integers, floats, and strings. And the data elements is associated with labels (index). In some situations, you need to convert a Pandas Series into different formats for various use cases like creating lists, NumPy arrays, dictionaries, or even converting the Series into a DataFrame.

In this tutorial, we will learn about various methods available in Pandas to convert a Series into different formats such as lists, NumPy arrays, dictionaries, DataFrames, and strings.

Following are the commonly used methods for converting Series into other formats −

Method Description
to_list() Converts the Series into a Python list.
to_numpy() Converts the Series into a NumPy array.
to_dict() Converts the Series into a dictionary.
to_frame() Converts the Series into a DataFrame.
to_string() Converts the Series into a string representation for display.

Converting Series to List

The Series.to_list() method converts a Pandas Series to a Python list, where each element of the Series becomes an element of the returned list. And the type of each element in the list is types as those in the Series.

Example

Here is the example of converting a Pandas Series into a Python list Using the Series.to_list() method.

Open Compiler
import pandas as pd # Create a Pandas Series s = pd.Series([1, 2, 3]) # Convert Series to a Python list result = s.to_list() print("Output:",result) print("Output Type:", type(result))

Following is the output of the above code −

Output: [1, 2, 3]
Output Type: <class 'list'>

Converting Series to NumPy Array

The Pandas Series.to_numpy() method can be used to convert a Pandas Series into a NumPy array. This method provides a additional features like specifying the data type (dtype), handle missing values (na_value), and control whether the result should be a copy or a view.

Example

This example converts a Series into a NumPy array using the Series.to_numpy() method.

Open Compiler
import pandas as pd # Create a Pandas Series s = pd.Series([1, 2, 3]) # Convert Series to a NumPy Array result = s.to_numpy() print("Output:",result) print("Output Type:", type(result))
Output: [1, 2, 3]
Output Type: <class 'numpy.ndarray'>

Converting Pandas Series to a Dictionary

The Pandas Series.to_dict() method is used to convert a Series into a Python dictionary, where each label (index) becomes a key and each corresponding value becomes the dictionary's value.

Example

The following example converts a Series into a Python dictionary using the Series.to_dict() method.

Open Compiler
import pandas as pd # Create a Pandas Series s = pd.Series([1, 2, 3], index=['a', 'b', 'c']) # Convert Series to a Python dictionary result = s.to_dict() print("Output:",result) print("Output Type:", type(result))
Output: {'a': 1, 'b': 2, 'c': 3}
Output Type: <class 'dict'>

Converting a Series to DataFrame

The Series.to_frame() method allows you to convert a Series into a DataFrame. Each Series becomes a single column in the DataFrame. This method provides a name parameter to set the column name of the resulting DataFrame.

Example

This example uses the Series.to_frame() method to convert a Series into a Pandas DataFrame with a single column.

Open Compiler
import pandas as pd # Create a Pandas Series s = pd.Series([1, 2, 3], index=['a', 'b', 'c']) # Convert Series to a Pandas DataFrame result = s.to_frame(name='Numbers') print("Output:\n",result) print("Output Type:", type(result))
Output:    
Numbers
a 1
b 2
c 3
Output Type: <class 'pandas.core.frame.DataFrame'>

Converting Series to Python String

To convert a Pandas Series object to a Python string you can use the he Series.to_string() method, which renders a string representation of the Series.

This method returns a string showing the index and values of the Series. You can customize the output string using various parameters like na_rep (represent missing values), header, index, float_format, length, etc.

Example

This example converts a Series into the Python string representation using the Series.to_string() method.

Open Compiler
import pandas as pd # Create a Pandas Series s = pd.Series([1, 2, 3], index=['r1', 'r2', 'r3']) # Convert Series to string representation result = s.to_string() print("Output:",repr(result)) print("Output Type:", type(result))
Output: 'r1    1\nr2    2\nr3    3'
Output Type: <class 'str'>
Advertisements