How to Convert Pandas Series to a DataFrame?

Pandas is a popular Python library for data analysis and manipulation. It provides two main data structures: Series and DataFrame. A Series is a 1-D array of values with an index that labels each element while a DataFrame is a 2-dimensional table of values with rows and columns that can store different types of data.

Sometimes to store more complex data or to perform more operations on data the Pandas series is converted into DataFrame. So here we are with possible methods. This post will explain the following ways to convert Pandas series to DataFrame in Python:

Method 1: Using pd.DataFrame() Function

The “pd.DataFrame()” function is utilized to create the 2-dimensional Pandas data frame object. The syntax of the “pd.DataFrame()” function is shown below:

pd.DataFrame(data=None, index=None, columns=None, dtype=None, copy=False)

From the above syntax:

  • The parameter “data” specifies the input data such as a dictionary, an array, a series, etc. that needs to be converted into DataFrame. 
  • The optional parameter “index” and “columns” are used for labeling the rows and columns.
  • The parameter “dtype” parameter indicates the data type for each column.
  • Lastly, the “copy” parameter specifies the Boolean value that is used to copy data from inputs.

Example 1: Convert Single Series to Pandas DataFrame

The below code uses the “pd.DataFrame()” function to convert the single series to Pandas DataFrame:

Code:

import pandas
series_value = pandas.Series(['Joseph', 'Alex', 'Lily', 'John', 'Anna'])
print(series_value)
print(type(series_value))
data_frame = pandas.DataFrame(series_value, columns =['Name'])
print('\n', data_frame)
print(type(data_frame))
  • The module named “pandas” is imported.
  • The “pd.Series()” function takes the data in the form of a list and converts it into a series.
  • The “pd.DataFrame()” function takes the series value and column name as a parameter and retrieves the Pandas DataFrame.
  • The “type()” function is used before and after conversion to verify the correct conversion of series to DataFrame.

Output:

The Pandas series has been converted into DataFrame.

Example 2: Convert Multiple Series to Pandas DataFrame

The following code converts multiple series to Pandas DataFrame:

Code:

import pandas
series_value1 = pandas.Series(['Joseph', 'Lily', 'Anna', 'Henry'])
series_value2 = pandas.Series([15, 25, 33, 15])
data_frame1= pandas.DataFrame(series_value1, columns=['Name'])
data_frame2 = pandas.DataFrame(series_value2, columns=['Age'])
data_frame = pandas.concat([data_frame1, data_frame2], axis=1)
print(data_frame)
print(type(data_frame))
  • The “pd.Series()” function is used multiple times in the above program to create multiple series data.
  • The “pd.DataFrame()” function is also used twice to convert each of the series into DataFrame and store in a variable “data_frame1” and “data_frame2”
  • The “pd.concat()” function is used to concatenate the two data frames along with the columns (axis=1). 

Output:

The multiple series have been converted into Pandas DataFrame.

Method 2: Using series.to_frame() Function

The “series.to_frame()” function is utilized to convert the series object into Pandas DataFrame. This function’s syntax is as follows:

series.to_frame(name=None)

The optional parameter “name” specifies the name of the dataframe column.

Example 1: Convert Single Series to Pandas DataFrame

The below examples are used to convert single series to Pandas DataFrame:

Code: 

import pandas
series_value = pandas.Series(['Joseph', 'Alex', 'Lily', 'John', 'Anna'])
print(series_value)
print(type(series_value))
data_frame = series_value.to_frame()
data_frame = data_frame.rename(columns = {0:'data'})
print('\n', data_frame)
print(type(data_frame))
  • The “pandas.Series()” function is used to create the series object from the given data.
  • The “series.to_frame()” function is used to convert the series object into DataFrame.
  • The “df.rename()” function is used to rename the DataFrame by adding a specified column name.

Output:

A Pandas DataFrame has been created from the single series.

Example 2: Convert Multiple Series to Pandas DataFrame

The multiple series is converted into Pandas DataFrame using the below code:

Code:

import pandas
series_value1 = pandas.Series(['Joseph', 'Lily', 'Anna', 'Henry'])
series_value2 = pandas.Series([15, 25, 33, 15])
data_frame1 = series_value1.to_frame()
data_frame1 = data_frame1.rename(columns = {0:'Name'})
data_frame2 = series_value2.to_frame()
data_frame2 = data_frame2.rename(columns = {0:'Age'})
data_frame = pandas.concat([data_frame1, data_frame2], axis=1)
print(data_frame)
print(type(data_frame))
  • The “pandas.Series()” function is used twice in the program to create the series object.
  • The “series.to_frame()” function is used to convert the initialized series into Pandas DataFrame.
  • The “pd.concat()” function is used to concatenate the two converted DataFrame.
  • The “df.rename()” function is used to rename the DataFrame by adding a specified column name.

Output: 

The multiple Pandas series objects have been converted into DataFrame.

Method 3: Using reset_index() Method

The “ reset_index() ” method is utilized to reset the index of the dataframe. The below code is used to convert the pandas series to DataFrame using the reset_index() method:

Code: 

import pandas as pd
series_value = pd.Series(index=['A', 'B', 'C'],data=['Joseph', 'Lily', 'Anna'],name='Name')
print(series_value)
print(type(series_value))
data_frame = series_value.reset_index()
print('\n',data_frame)
print(type(data_frame))
  • The “pd.series()” function creates the series of the specified data.
  • The “reset_index()” function is utilized to reset the index of the specified series and returns the dataframe.

Note: if the index is not specified in the series then the default index is assigned to the series.

Output:

The given series has been converted into DataFrame.

Conclusion

To convert the Pandas series to a DataFrame, the “pd.DataFrame()” function and the “series.to_frame()” function is used in Python. The “pd.DataFrame()” function accepts the series object as an argument and converts it into Pandas DataFame. For multiple series conversion into DataFrame, the “pd.concat()” function is used to concatenate the multiple DataFrame into one DataFrame. 

This guide presented various ways to convert the Pandas series to a DataFrame using numerous examples.