How to Convert Pandas Series to DataFrame (With Examples)

Pandas Series to DataFrame conversion is a common task performed by data professionals. It involves the conversion of a one-dimensional pandas Series object to a two-dimensional pandas DataFrame object. This can be accomplished by using the to_frame() method. This method takes the pandas Series object as an argument and returns a new DataFrame object. Examples of how to use this method are provided for illustration.


You can use the following basic syntax to convert a pandas Series to a pandas DataFrame:

my_df = my_series.to_frame(name='column_name')

The following examples show how to use this syntax in practice.

Example 1: Convert One Series to Pandas DataFrame

Suppose we have the following pandas Series:

import pandas as pd

#create pandas Series
my_series = pd.Series([3, 4, 4, 8, 14, 17, 20])

#view pandas Series
print(my_series)

0     3
1     4
2     4
3     8
4    14
5    17
6    20
dtype: int64

#view object type
print(type(my_series))

<class 'pandas.core.series.Series'>

We can use the to_frame() function to quickly convert this pandas Series to a pandas DataFrame:

#convert Series to DataFrame and specify column name to be 'values'
my_df = my_series.to_frame(name='values')

#view pandas DataFrame 
print(my_df)

   values
0       3
1       4
2       4
3       8
4      14
5      17
6      20

#view object type 
print(type(my_df))

<class 'pandas.core.frame.DataFrame'>

Example 2: Convert Multiple Series to Pandas DataFrame

Suppose we have three different pandas Series:

import pandas as pd

#define three Series
name = pd.Series(['A', 'B', 'C', 'D', 'E'])
points = pd.Series([34, 20, 21, 57, 68])
assists = pd.Series([8, 12, 14, 9, 11])

We can use the following syntax to convert each Series into a DataFrame and concatenate the three DataFrames into one final DataFrame:

#convert each Series to a DataFrame
name_df = name.to_frame(name='name')
points_df = points.to_frame(name='points')
assists_df = assists.to_frame(name='assists')

#concatenate three Series into one DataFrame
df = pd.concat([name_df, points_df, assists_df], axis=1)

#view final DataFrame
print(df)

  name  points  assists
0    A      34        8
1    B      20       12
2    C      21       14
3    D      57        9
4    E      68       11

The final result is a pandas DataFrame where each Series represents a column.

The following tutorials explain how to perform other common data object conversions in pandas:

x