Convert Series to pandas DataFrame in Python (2 Examples)


In this tutorial, I’ll illustrate how to convert a pandas Series to a DataFrame in the Python programming language.

Table of contents:

If you want to learn more about these topics, keep reading…


Example Data & Software Libraries

First, we need to import the pandas library:

import pandas as pd                       # Load pandas

The pandas Series below is used as a basis for this Python tutorial:

my_ser = pd.Series([1, 3, 5, 7, 9])       # Create example Series
print(my_ser)                             # Print example Series
# 0    1
# 1    3
# 2    5
# 3    7
# 4    9
# dtype: int64

As you can see, our pandas Series consists of five integer elements.


Example 1: Create pandas DataFrame from Series Using to_frame() Function

Example 1 illustrates how to convert a pandas Series to a pandas DataFrame in Python.

For this task, we can use the to_frame function as shown in the following Python syntax:

my_data1 = my_ser.to_frame('x')           # Convert Series to DataFrame
print(my_data1)                           # Print pandas DataFrame


table 1 DataFrame convert series pandas dataframe python


As you can see based on Table 1, we have created a new pandas DataFrame from our Series object composed of five rows and one column. The values in this data set correspond to the values in our Series.


Example 2: Create pandas DataFrame from Series Using DataFrame() Function

Alternatively to the to_frame function that we have used in Example 1, we can also apply the DataFrame function of the pandas library to transform a Series object to a DataFrame.

To accomplish this, we can use the Python code that you can see below:

my_data2 = pd.DataFrame({'x': my_ser})    # Convert Series to DataFrame
print(my_data2)                           # Print pandas DataFrame


table 2 DataFrame convert series pandas dataframe python


By running the previous Python code, we have created Table 2, i.e. another pandas DataFrame containing our input series as pandas DataFrame variable.


Video & Further Resources

Do you want to know more about the conversion of a pandas Series to a DataFrame column? Then you may want to watch the following video which I have published on my YouTube channel. In the video, I’m explaining the contents of this article:



Besides that, you may want to have a look at the related tutorials which I have published on this website.


Summary: This page has shown how to change and transform a pandas Series to a DataFrame column in Python. Let me know in the comments, in case you have additional questions.


Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.