Pandas – Convert a Pandas Series to a DataFrame

Spread the love

You have a pandas series and you want to convert it to a pandas dataframe.

Solution –

Create a Pandas Series –

Let’s create a pandas series from a list. For more information read this post – Create a pandas series from a list.

# import pandas 
import pandas as pd

# list of corporation names
corporations = ['Exxon Mobil','Walmart','Chevron','ConocoPhillips','General Electric']

corp_series = pd.Series(corporations)
print(corp_series)
print("Data Type:", type(corp_series))

Convert a Pandas Series to a DataFrame –

To convert this series to a pandas dataframe, we can use the pandas.Series.to_frame() function.

# convert a series to a dataframe
df1 = corp_series.to_frame()
print(df1)
print("Data Type:", type(df1))

Alternatively, You can also directly pass the series to the pd.DataFrame constructor.

# convert a series to a dataframe
df2 = pd.DataFrame(corp_series)
print(df2)
print("Data Type:", type(df2))

Convert Multiple Series to a Pandas DataFrame –

Sometimes you might have more than 1 series and you want to convert them.

# list of corporation names
corporations = ['Exxon Mobil','Walmart','Chevron','ConocoPhillips','General Electric']

# list of their revenues in billion dollars
revenues = [443, 406, 263, 231, 149]

# corporation series
corp_series = pd.Series(corporations)

# revenue series
revenue_series = pd.Series(revenues)

Now, to convert these series into a dataframe, we can use a dictionary whose keys will be the column names and the values will be the values of the series and then pass that dictionary to the pd.DataFrame.

# create dataframe from multiple series
df = pd.DataFrame({'Corporation': corp_series,
                  'Revenue': revenue_series})
print(df)
print("Data Type:", type(df))

So, if you use a dictionary then it does not matter if you have 1 series or 2 or 100. You can easily convert them into a dataframe without converting each series to a dataframe then concatenating each of them together to create a single dataframe. Using a dictionary is much easier and clean.

Using pandas.Series.reset_index –

Sometimes you may want to promote a series to a dataframe because the index is meaningful and you want to treat it as a column in your dataframe.

Let’s say that you have a series which contains the revenue data and the name of the companies is in the index. To convert this series we can use the Series.reset_index function.

# revenue data in a list
revenues = [443, 406, 263, 231, 149]
# index values
index = ['Exxon Mobil','Walmart','Chevron','ConocoPhillips','General Electric']

# create a series
s1 = pd.Series(revenues, index=index)

print(s1)
print("Data Type:", type(s1))
print(s1.index)

At this moment the company names are the index of the series and the data type of s1 is series. Now, let’s apply the reset_index function.

# convert the series to a dataframe
df = s1.reset_index()
print(df)
print(type(df))

The data type has been successfully converted into a dataframe and we also transformed the index into a column. But if you look at the column names, you can see that they are not very descriptive. We can fix this issue using the rename method.

# convert series to a dataframe
df = s1.reset_index().rename(columns={'index':'Corporation', 0:'Revenue'})
print(df)
print(type(df))

If you liked this post then please share it with others and subscribe to our blog to learn more about pandas and data science.

  1. Create a Pandas Series from a dictionary.
  2. Create a Pandas Series from a list.
  3. Create Pandas DataFrame from dictionary.
  4. Create Pandas DataFrame from Lists.
  5. Convert a Pandas DataFrame to a Dictionary.

Rating: 1 out of 5.

Leave a Reply