First of all, let we understand that what are pandas series . Pandas Series are the type of array data structure. It is one dimensional data structure. It is capable of holding data of any type such as string, integer, float etc. A Series can be created using Series constructor.
Syntax: pandas.Series(data, index, dtype, copy)
Return: Series object.
Now, Let’s create a pandas Series:
Python3
import pandas as pd
s = pd.Series(['C', 'C++', 'Java',
'Python', 'Perl', 'Ruby',
'Julia'])
s
Output:
Now, we will convert given Pandas series into a dataframe with its index as another column on the dataframe by using Series.to_frame() and Dataframe.reset_index() methods together.
Syntax: Series.to_frame(name=None)
Return: Dataframe.
Syntax: Dataframe.reset_index(level=None, drop=False, name=None, inplace=False)
Return: Dataframe.
Example 1: We will convert given Pandas series into a dataframe with its index as another column.
Python3
df = s.to_frame().reset_index()
df
Output:
Example 2: We can also rename our index column.
Python3
df.rename(columns = {'index':'new_index'},
inplace = True)
df
Output:
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills, become a part of the hottest trend in the 21st century.Dive into the future of technology - explore the
Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated :
17 Aug, 2020
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...