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 = 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 a 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 to 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 DataFrame

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

import pandas as pd

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

df = pd.DataFrame(my_series)

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 assign a new column name by adding columns = [‘First Name’] to the code:

import pandas as pd

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

df = pd.DataFrame(my_series, columns = ['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 NameLast NameAge
JonAnderson23
MarkSmith57
MariaGreen42
JillMiller35
JackCarter29

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, columns = ['First Name'])
df_last_name = pd.DataFrame(series_last_name, columns = ['Last Name'])
df_age = pd.DataFrame(series_age, columns = ['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 may also want to check the following guides for the steps to: