How to Convert Pandas Series to a DataFrame

In this tutorial, you’ll see how to convert Pandas Series to a DataFrame. You’ll also observe how to convert multiple Series into a DataFrame.

To begin, here is the syntax that you may use to convert your Series to a DataFrame:

df = my_series.to_frame()

Alternatively, you can use this approach to convert your Series:

df = pd.DataFrame(my_series)

In the next section, you’ll see how to apply the above syntax using a simple example.

Steps to Convert Pandas Series to DataFrame

Step 1: Create a Series

To start with a simple example, let’s create Pandas Series from a List of 5 individuals:

import pandas as pd

first_name = ['Jon','Mark','Maria','Jill','Jack']
my_series = pd.Series(first_name)

print(my_series)
print(type(my_series))

Run the code in Python, and you’ll get the following Series:

How to Convert Pandas Series to a DataFrame

Note that the syntax of print(type(my_series)) was added at the bottom of the code in order to demonstrate that we created a Series (as highlighted in red above).

Step 2: Convert the Pandas Series to a DataFrame

Next, convert the Series to a DataFrame by adding df = my_series.to_frame() to the code:

import pandas as pd

first_name = ['Jon','Mark','Maria','Jill','Jack']
my_series = pd.Series(first_name)

df = my_series.to_frame()

print(df)
print(type(df))

Run the code, and you’ll now get the DataFrame:

Example of Data

In the above case, the column name is ‘0.’ Alternatively, you may rename the column by adding df = df.rename(columns = {0:’First Name’}) to the code:

import pandas as pd

first_name = ['Jon','Mark','Maria','Jill','Jack']
my_series = pd.Series(first_name)

df = my_series.to_frame()
df = df.rename(columns = {0:'First Name'})

print(df)
print(type(df))

You’ll now see the new column name at the top:

How to Convert Pandas Series to a DataFrame

Convert Multiple Series to Pandas DataFrame

Now you’ll observe how to convert multiple Series (for the following data) into a DataFrame.

First Name Last Name Age
Jon Anderson 23
Mark Smith 57
Maria Green 42
Jill Miller 35
Jack Carter 29

Let’s now create the 3 Series based on the above data:

import pandas as pd

first_name = ['Jon','Mark','Maria','Jill','Jack']
series_first_name = pd.Series(first_name)
print (series_first_name)

last_name = ['Anderson','Smith','Green','Miller','Carter']
series_last_name = pd.Series(last_name)
print(series_last_name)

age = [23,57,42,35,29]
series_age = pd.Series(age)
print(series_age)

Run the code, and you’ll get the following 3 Series:

3 Pandas Series

In order to convert the 3 Series into a DataFrame, you’ll need to:

  • Convert the 3 Series into 3 DataFrames
  • Concatenate the 3 DataFrames into a single DataFrame

Here is the complete code:

import pandas as pd

first_name = ['Jon','Mark','Maria','Jill','Jack']
series_first_name = pd.Series(first_name)

last_name = ['Anderson','Smith','Green','Miller','Carter']
series_last_name = pd.Series(last_name)

age = [23,57,42,35,29]
series_age = pd.Series(age)

df_first_name = pd.DataFrame(series_first_name)
df_first_name = df_first_name.rename(columns = {0:'First Name'})

df_last_name = pd.DataFrame(series_last_name)
df_last_name = df_last_name.rename(columns = {0:'Last Name'})

df_age = pd.DataFrame(series_age)
df_age = df_age.rename(columns = {0:'Age'})

df_all = pd.concat([df_first_name, df_last_name, df_age], axis=1)
print(df_all)
print(type(df_all))

Once you run the code, you’ll get this single DataFrame:

Pandas Series to a DataFrame

Additional Resources

You can visit the Pandas Documentation to learn more about to_frame().

You may also want to check the following guides for the steps to: