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 items:

import pandas as pd

item = ['Computer', 'Printer', 'Tablet', 'Desk', 'Chair']
my_series = pd.Series(item)

print(my_series)
print(type(my_series))

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

0    Computer
1     Printer
2      Tablet
3        Desk
4       Chair
dtype: object
<class 'pandas.core.series.Series'>

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 yellow 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

item = ['Computer', 'Printer', 'Tablet', 'Desk', 'Chair']
my_series = pd.Series(item)

df = my_series.to_frame()

print(df)
print(type(df))

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

          0
0  Computer
1   Printer
2    Tablet
3      Desk
4     Chair
<class 'pandas.core.frame.DataFrame'>

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

import pandas as pd

item = ['Computer', 'Printer', 'Tablet', 'Desk', 'Chair']
my_series = pd.Series(item)

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

print(df)
print(type(df))

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

       item
0  Computer
1   Printer
2    Tablet
3      Desk
4     Chair
<class 'pandas.core.frame.DataFrame'>

Convert Multiple Series to Pandas DataFrame

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

item brand price
Computer A 700
Printer B 150
Tablet C 300
Desk D 450
Chair E 200

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

import pandas as pd

item = ['Computer', 'Printer', 'Tablet', 'Desk', 'Chair']
series_item = pd.Series(item)
print (series_item)

brand = ['A', 'B', 'C', 'D', 'E']
series_brand = pd.Series(brand)
print(series_brand)

price = [700, 150, 300, 450, 200]
series_price = pd.Series(price)
print(series_price)

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

0    Computer
1     Printer
2      Tablet
3        Desk
4       Chair
dtype: object
0    A
1    B
2    C
3    D
4    E
dtype: object
0    700
1    150
2    300
3    450
4    200
dtype: int64

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

item = ['Computer', 'Printer', 'Tablet', 'Desk', 'Chair']
series_item = pd.Series(item)

brand = ['A', 'B', 'C', 'D', 'E']
series_brand = pd.Series(brand)

price = [700, 150, 300, 450, 200]
series_price = pd.Series(price)

df_item= pd.DataFrame(series_item)
df_item = df_item.rename(columns = {0:'item'})

df_brand = pd.DataFrame(series_brand)
df_brand = df_brand.rename(columns = {0:'brand'})

df_price = pd.DataFrame(series_price)
df_price = df_price.rename(columns = {0:'price'})

df_all = pd.concat([df_item, df_brand, df_price ], axis=1)
print(df_all)
print(type(df_all))

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

       item  brand  price
0  Computer      A    700
1   Printer      B    150
2    Tablet      C    300
3      Desk      D    450
4     Chair      E    200
<class 'pandas.core.frame.DataFrame'>

Additional Resources

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

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