Convert Pandas Series to a DataFrame

In this tutorial, we will look at how to convert a pandas series to a dataframe.

There are a number of ways to get a dataframe from a series. You can use the to_frame() function, the reset_index() function, or simply create a new dataframe using the values and index of the pandas series. The following is the syntax to use the above functions:

# using to_frame()
df = s.to_frame()
# using reset_index()
df = s.reset_index()

Here, s is the pandas series you want to convert. Both to_frame() and reset_index() return different dataframes. The to_frame() function retains the index of the series as the index of the dataframe whereas the reset_index() function creates a dataframe with the index of the series as a separate column. (See the examples below)

Let’s look at some examples of using the above methods to create a dataframe from a series. First, we’ll create a sample pandas series which we will be using throughout this tutorial.

import pandas as pd

# pandas series Wimbledon winners from 2015 to 2019
wimbledon_winners = pd.Series(index=[2015, 2016, 2017, 2018, 2019],
                   data=['Novak Djokovic', 'Andy Murray', 'Roger Federer', 'Novak Djokovic', 'Novak Djokovic'],
                   name='Name')

# display the series
print(wimbledon_winners)

Output:

2015    Novak Djokovic
2016       Andy Murray
2017     Roger Federer
2018    Novak Djokovic
2019    Novak Djokovic
Name: Name, dtype: object

You can see the contents of the series object above. Let’s confirm the type of the object.

# check the type
print(type(wimbledon_winners))

Output:

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

We now have a pandas series containing the name of Wimbledon Winners from 2015 to 2019 with the year as its index.

First, let’s see the usage of the to_frame() function to get a pandas dataframe from a series.

df = wimbledon_winners.to_frame()
# check the type
print(type(df))

Output:

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

Let’s check how the dataframe looks.

df

Output:

Resulting dataframe from the to_frame() function

Notice that when using the to_frame() function, the resulting dataframe retains the index as was present in the series.

For more on the to_frame() function, refer to its documentation.

The reset_index() function is generally used to reset the index of a dataframe. But, you can also use it to get a dataframe from a pandas series.

df = wimbledon_winners.reset_index()
# check the type
print(type(df))

Output:

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

Let’s check how the dataframe looks.

df
The resulting dataframe from the reset_index() function

You can see that the resulting dataframe has two columns, one for the series values and the other for the original index of the series. You can also separately set any column as your dataframe index using the set_index() function.

df = wimbledon_winners.reset_index().set_index('index')
print(type(df))

Output:

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

Let’s see how the dataframe looks.

df

Output:

The resulting dataframe from using the reset_index() function and setting the index back.

With this, we come to the end of this tutorial. The code examples and results presented in this tutorial have been implemented in a Jupyter Notebook with a python (version 3.8.3) kernel having pandas version 1.0.5


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.