Pandas Series.to_frame() Function

  1. Syntax of pandas.Series.to_frame():
  2. Example Codes: Series.to_frame() Method to Convert a Series to a Dataframe
  3. Example Codes: Series.to_frame() Method to Convert a Series to a Dataframe With a Specific Column Name

Python Pandas Series.to_frame() function converts the given series to a data frame.

Syntax of pandas.Series.to_frame():

Series.to_frame(name=None)

Parameters

name It is an object type parameter. It tells that if series has a name, then the passed name should substitute for the series name.

Return

It returns a data frame that represents the series.

Example Codes: Series.to_frame() Method to Convert a Series to a Dataframe

import pandas as pd

series = pd.Series([ 'Rose', 'Jasmine', 'Lili', 'Tulip', 'Hibiscus', 'Sun Flower', 'Orchid', 'Carnation','Irises', 'Gardenias'])
print("The Original Series is: \n")
print(series)

dataframe = series.to_frame()
print("The Dataframe is: \n")
print(dataframe)

Output:

The Original Series is: 

0          Rose
1       Jasmine
2          Lili
3         Tulip
4      Hibiscus
5    Sun Flower
6        Orchid
7     Carnation
8        Irises
9     Gardenias
dtype: object
The Dataframe is: 

            0
0        Rose
1     Jasmine
2        Lili
3       Tulip
4    Hibiscus
5  Sun Flower
6      Orchid
7   Carnation
8      Irises
9   Gardenias

The function has returned the data frame that represents the given series.

Example Codes: Series.to_frame() Method to Convert a Series to a Dataframe With a Specific Column Name

import pandas as pd

series = pd.Series([ 'Rose', 'Jasmine', 'Lili', 'Tulip', 'Hibiscus', 'Sun Flower', 'Orchid', 'Carnation','Irises', 'Gardenias'])
print("The Original Series is: \n")
print(series)

dataframe = series.to_frame(name= 'Flowers')
print("The Dataframe is: \n")
print(dataframe)

Output:

The Original Series is: 

0          Rose
1       Jasmine
2          Lili
3         Tulip
4      Hibiscus
5    Sun Flower
6        Orchid
7     Carnation
8        Irises
9     Gardenias
dtype: object
The Dataframe is: 

      Flowers
0        Rose
1     Jasmine
2        Lili
3       Tulip
4    Hibiscus
5  Sun Flower
6      Orchid
7   Carnation
8      Irises
9   Gardenias

Now the column name is Flowers.

Contribute
DelftStack is a collective effort contributed by software geeks like you. If you like the article and would like to contribute to DelftStack by writing paid articles, you can check the write for us page.

Related Article - Pandas Series

  • Pandas Series.tolist() Function