Pandas series and dataframes are the two data structures that we use extensively to manipulate sequential or tabular data in Python. Sometimes, we need to convert one or multiple series objects to a dataframe in Python. This article discusses how to convert a pandas series to a dataframe in python.
- Convert Pandas Series to DataFrame Using the to_frame() Method
- Pandas Series to DataFrame Using the DataFrame() Function
- Convert the index of the series to a column in dataframe
- Convert the Index of A Series to Columns in The DataFrame Using the DataFrame() Function
- Multiple Pandas Series to DataFrame in Python
- Conclusion
Convert Pandas Series to DataFrame Using the to_frame() Method
We can convert a pandas series to a dataframe using the to_frame()
method. The to_frame()
method, when invoked on a series object, returns the dataframe representation of the series. You can observe this in the following example.
import pandas as pd
numbers=[100,90,80,90,70,100,60]
series=pd.Series(numbers)
print("The series is:")
print(series)
df=series.to_frame()
print("The dataframe is:")
print(df)
Output:
The series is:
0 100
1 90
2 80
3 90
4 70
5 100
6 60
dtype: int64
The dataframe is:
0
0 100
1 90
2 80
3 90
4 70
5 100
6 60
In the above example, we have first converted a list into a series using the Series()
function. Then, we used the to_frame()
method to convert the series to a dataframe. Here, the input series doesn’t contain any index.
If the input series contains indices, they are converted to indices of the dataframe. You can observe this in the following example.
import pandas as pd
numbers=[100,90,80,90,70,100,60]
indices=[1,2,4,5,6,7,8]
series=pd.Series(numbers,index=indices)
print("The series is:")
print(series)
df=series.to_frame()
print("The dataframe is:")
print(df)
Output:
The series is:
1 100
2 90
4 80
5 90
6 70
7 100
8 60
dtype: int64
The dataframe is:
0
1 100
2 90
4 80
5 90
6 70
7 100
8 60
In this example, we first created a series having custom indices. Then, we converted it into a dataframe using the to_frame()
method. You can observe that the indices of the series have been converted into dataframe index.
When we convert a pandas series to a dataframe using the to_frame()
method, the newly created column in the dataframe is named “0”
.
If you want to name the column explicitly, you can pass the column name to the name
parameter in the to_frame()
method. After execution, the to_frame()
method will return a dataframe with the specified column name. You can observe this in the following example.
import pandas as pd
numbers=[100,90,80,90,70,100,60]
indices=[1,2,4,5,6,7,8]
series=pd.Series(numbers,index=indices)
print("The series is:")
print(series)
df=series.to_frame(name="Numbers")
print("The dataframe is:")
print(df)
Output:
The series is:
1 100
2 90
4 80
5 90
6 70
7 100
8 60
dtype: int64
The dataframe is:
Numbers
1 100
2 90
4 80
5 90
6 70
7 100
8 60
In this example, we passed the string "Numbers"
to the name parameter in the to_frame()
method. Hence, the output dataframe contains the column "Numbers"
instead of "0"
.
Pandas Series to DataFrame Using the DataFrame() Function
Instead of the to_frame()
method, you can also use the DataFrame()
function to convert a pandas series to a dataframe. The DataFrame()
function takes the series as its input argument and returns the output dataframe as shown below.
import pandas as pd
numbers=[100,90,80,90,70,100,60]
series=pd.Series(numbers)
print("The series is:")
print(series)
df=pd.DataFrame(series)
print("The dataframe is:")
print(df)
Output:
The series is:
0 100
1 90
2 80
3 90
4 70
5 100
6 60
dtype: int64
The dataframe is:
0
0 100
1 90
2 80
3 90
4 70
5 100
6 60
In the above example, you can observe that the column name of the output dataframe is set to "0"
. To give a custom column name to the output dataframe, you can pass the column name to the columns
parameter in the DataFrame()
function as shown below.
import pandas as pd
numbers=[100,90,80,90,70,100,60]
series=pd.Series(numbers)
print("The series is:")
print(series)
df=pd.DataFrame(series,columns=["Numbers"])
print("The dataframe is:")
print(df)
Output:
The series is:
0 100
1 90
2 80
3 90
4 70
5 100
6 60
dtype: int64
The dataframe is:
Numbers
0 100
1 90
2 80
3 90
4 70
5 100
6 60
If the input series contains custom indices, the indices are copied into the output dataframe. You can observe this in the following example.
import pandas as pd
numbers=[100,90,80,90,70,100,60]
indices=[1,2,4,5,6,7,8]
series=pd.Series(numbers,index=indices)
print("The series is:")
print(series)
df=pd.DataFrame(series,columns=["Numbers"])
print("The dataframe is:")
print(df)
Output:
The series is:
1 100
2 90
4 80
5 90
6 70
7 100
8 60
dtype: int64
The dataframe is:
Numbers
1 100
2 90
4 80
5 90
6 70
7 100
8 60
Convert the index of the series to a column in dataframe
In the above examples, you might have observed that the indices of the input series are converted to the index of the dataframe. If you want to convert the indices of the input series to a column in the output dataframe, you can use the reset_index()
method.
The reset_index()
method, when invoked on a series, converts the index of the series into a column and returns the resulting dataframe as shown below.
import pandas as pd
numbers=[100,90,80,90,70,100,60]
indices=[1,2,4,5,6,7,8]
series=pd.Series(numbers,index=indices)
print("The series is:")
print(series)
df=series.reset_index()
print("The dataframe is:")
print(df)
Output:
The series is:
1 100
2 90
4 80
5 90
6 70
7 100
8 60
dtype: int64
The dataframe is:
index 0
0 1 100
1 2 90
2 4 80
3 5 90
4 6 70
5 7 100
6 8 60
In this example, you can observe that the index of the series is converted to the column “index”
in the output dataframe. However, the column containing values is named "0"
. To set the name of the column containing values, you can pass the name of the output column to the name
parameter in the reset_index()
method as shown in the following example.
import pandas as pd
numbers=[100,90,80,90,70,100,60]
indices=[1,2,4,5,6,7,8]
series=pd.Series(numbers,index=indices)
print("The series is:")
print(series)
df=series.reset_index(name="Numbers")
print("The dataframe is:")
print(df)
Output:
The series is:
1 100
2 90
4 80
5 90
6 70
7 100
8 60
dtype: int64
The dataframe is:
index Numbers
0 1 100
1 2 90
2 4 80
3 5 90
4 6 70
5 7 100
6 8 60
Convert the Index of A Series to Columns in The DataFrame Using the DataFrame() Function
To convert the index of a series to a column in the dataframe with desired column names, we will use the following steps.
- First, we will obtain a list of indices using the
index
attribute of the series. - Then, we will obtain a list of values using the
values
attribute of the series. - After this, we will create a python dictionary using the above lists. Here, we will use the column names of the desired dataframe as keys in the dictionary and the list of indices and values as the associated values.
- After creating the dictionary, we will convert the dictionary into a dataframe using the
DataFrame()
function. TheDataFrame()
function takes a list containing the dictionary as its input argument and returns the output dataframe.
After executing the above steps, you will get the desired output dataframe. You can observe this in the following example.
import pandas as pd
numbers=[100,90,80,90,70,100,60]
indices=["A","B","C","D","E","F","G"]
series=pd.Series(numbers,index=indices)
print("The series is:")
print(series)
values=series.values
index_values=series.index
myDict=dict()
for i in range(len(values)):
key=index_values[i]
value=values[i]
myDict[key]=value
print("The dictionary is:")
print(myDict)
df=pd.DataFrame([myDict])
print("The dataframe is:")
print(df)
Output:
The series is:
A 100
B 90
C 80
D 90
E 70
F 100
G 60
dtype: int64
The dictionary is:
{'A': 100, 'B': 90, 'C': 80, 'D': 90, 'E': 70, 'F': 100, 'G': 60}
The dataframe is:
A B C D E F G
0 100 90 80 90 70 100 60
Multiple Pandas Series to DataFrame in Python
Instead of a single series, we can also convert multiple series into a dataframe. In this case, we can convert the series into rows or columns of the output dataframe. Let us discuss both these approaches.
Convert Multiple Series Objects into Rows of DataFrame
To convert multiple series into rows of a pandas dataframe, you can use the DataFrame()
function. The DataFrame()
function takes a list of series objects as its input argument and returns a dataframe as output.
You can observe this in the following example.
import pandas as pd
numbers1=[100,90,80,90,70,100,60]
numbers2=[1,2,3,4,5,6,7]
series1=pd.Series(numbers1)
series2=pd.Series(numbers2)
print("The first series is:")
print(series1)
print("The second series is:")
print(series2)
df=pd.DataFrame([series1,series2])
print("The dataframe is:")
print(df)
Output:
The first series is:
0 100
1 90
2 80
3 90
4 70
5 100
6 60
dtype: int64
The second series is:
0 1
1 2
2 3
3 4
4 5
5 6
6 7
dtype: int64
The dataframe is:
0 1 2 3 4 5 6
0 100 90 80 90 70 100 60
1 1 2 3 4 5 6 7
Convert Multiple Series into Columns of DataFrame
To convert multiple series into columns of a pandas dataframe, we will use the concat()
function defined in the pandas module. Here, we will pass the list of series to the concat()
function as its first input argument. Additionally, we will set the axis
parameter to 1. After execution of the concat()
function, we will get the output dataframe with all the input series objects as columns of the dataframe.
You can observe this in the following example.
import pandas as pd
numbers1=[100,90,80,90,70,100,60]
numbers2=[1,2,3,4,5,6,7]
series1=pd.Series(numbers1)
series2=pd.Series(numbers2)
print("The first series is:")
print(series1)
print("The second series is:")
print(series2)
df=pd.concat([series1,series2],axis=1)
print("The dataframe is:")
print(df)
Output:
The first series is:
0 100
1 90
2 80
3 90
4 70
5 100
6 60
dtype: int64
The second series is:
0 1
1 2
2 3
3 4
4 5
5 6
6 7
dtype: int64
The dataframe is:
0 1
0 100 1
1 90 2
2 80 3
3 90 4
4 70 5
5 100 6
6 60 7
Conclusion
In this article, we have discussed different ways to convert a pandas series to a dataframe. We also discussed how to convert multiple series objects to dataframe in python. To learn more about pandas dataframes, you can read this article on how to assign column to a dataframe in python. You might also like this article on how to check for null values in pandas.
I hope you enjoyed reading this article. Stay tuned for more informative articles.
Happy Learning!
Recommended Python Training
Course: Python 3 For Beginners
Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.