Pandas Series.to_frame() Function
Minahil Noor
Jan 30, 2023
-
Syntax of
pandas.Series.to_frame()
: -
Example Codes:
Series.to_frame()
Method to Convert a Series to a Dataframe -
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 DataFrame.
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 DataFrame 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 DataFrame 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
.