Movatterモバイル変換


[0]ホーム

URL:


Python Pandas Tutorial

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 −

MethodDescription
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

TheSeries.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 theSeries.to_list() method.

import pandas as pd# Create a Pandas Seriess = pd.Series([1, 2, 3])# Convert Series to a Python listresult = 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 PandasSeries.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 theSeries.to_numpy() method.

import pandas as pd# Create a Pandas Seriess = pd.Series([1, 2, 3])# Convert Series to a NumPy Arrayresult = 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 PandasSeries.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 theSeries.to_dict() method.

import pandas as pd# Create a Pandas Seriess = pd.Series([1, 2, 3], index=['a', 'b', 'c'])# Convert Series to a Python dictionaryresult = 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

TheSeries.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 theSeries.to_frame() method to convert a Series into a Pandas DataFrame with a single column.

import pandas as pd# Create a Pandas Seriess = pd.Series([1, 2, 3], index=['a', 'b', 'c'])# Convert Series to a Pandas DataFrameresult = s.to_frame(name='Numbers')print("Output:\n",result)print("Output Type:", type(result))
Output:
Numbers
a1
b2
c3
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 heSeries.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 likena_rep (represent missing values), header, index, float_format, length, etc.

Example

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

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

[8]ページ先頭

©2009-2025 Movatter.jp