Convert Pandas Series to DataFrame

To convert your Series to a DataFrame:

df = ser.to_frame()

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

df = pd.DataFrame(ser)

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

import pandas as pd

item = ["Computer", "Printer", "Tablet", "Desk", "Chair"]
ser = pd.Series(item)

print(ser)
print(type(ser))

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 “print(type(ser))” 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 = ser.to_frame() to the code:

import pandas as pd

item = ["Computer", "Printer", "Tablet", "Desk", "Chair"]
ser = pd.Series(item)

df = ser.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 to “item” by adding df.rename(columns={0: “item”}, inplace=True) to the code:

import pandas as pd

item = ["Computer", "Printer", "Tablet", "Desk", "Chair"]
ser = pd.Series(item)

df = ser.to_frame()
df.rename(columns={0: "item"}, inplace=True)

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.

itembrandprice
ComputerA700
PrinterB150
TabletC300
DeskD450
ChairE200

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.rename(columns={0: "item"}, inplace=True)

df_brand = pd.DataFrame(series_brand)
df_brand.rename(columns={0: "brand"}, inplace=True)

df_price = pd.DataFrame(series_price)
df_price.rename(columns={0: "price"}, inplace=True)

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:

Leave a Comment