In pandas, converting a series to a DataFrame is a straightforward process.
pandas uses the to_frame()
method to easily convert a series into a data frame.
Series.to_frame(name=None)
name
:
None
Series
Let’s convert a pandas series s
into a data frame and set the column name to 'Age'
:
import pandas as pds = pd.Series([25, 18, 40], name="vals")df = s.to_frame(name="Age")print(df)print(type(df))