. Advertisement .
..3..
. Advertisement .
..4..
Are you looking for a way to convert Pandas Series to DataFrame? You are at the right place. This guide with specific examples will teach you how to do so. We also cover how to convert many Pandas Series into one DataFrame.
What is a pandas series? It is a one-dimensional array with axis labels. These labels have to be the hashable type, but they do not need to be unique. The object can support both label-based and integer-based indexing. It also offers a variety of ways to carry out actions involving the index.
The function Series.to frame() in Pandas is used to turn the specified series of objects into a dataframe. The syntax you can use to change your Series into a DataFrame is as follows to get started:
df = my_series.to_frame()
As an alternative, you can convert the Series using this method:
df = pd.DataFrame(my_series)
Convert Pandas Series To Dataframe Step By Step
Below are the complete steps for applying the syntax above.
1. Create A Pandas Series
Now, let’s establish the Pandas Series from the List of five items as a quick example to get things started:
import pandas as pd
item = [‘Computer’, ‘Printer’, ‘Tablet’, ‘Desk’, ‘Chair’]
my_series = pd.Series(item)
print(my_series)
print(type(my_series))
Execute this code, and you will receive the Series as follow:
0 Computer
1 Printer
2 Tablet
3 Desk
4 Chair
dtype: object
<class ‘pandas.core.series.Series’>
Keep in mind that the print(type(my series))
syntax was included at the end of your code to show that you constructed a Series.
2. Convert Pandas Series To Dataframe
Add df = my series.to frame()
to your code to turn the Series into a DataFrame:
import pandas as pd
item = [‘Computer’, ‘Printer’, ‘Tablet’, ‘Desk’, ‘Chair’]
my_series = pd.Series(item)
df = my_series.to_frame()
print(df)
print(type(df))
After running the code, the below DataFrame will appear:
0
0 Computer
1 Printer
2 Tablet
3 Desk
4 Chair
<class ‘pandas.core.frame.DataFrame’>
The column name in the example above is “0.” As an alternative, you can rename this column by including df = df.rename(columns = {0:'item'})
to your code.
import pandas as pd
item = [‘Computer’, ‘Printer’, ‘Tablet’, ‘Desk’, ‘Chair’]
my_series = pd.Series(item)
df = my_series.to_frame()
df = df.rename(columns = {0:’item’})
print(df)
print(type(df))
The name of the column is now visible:
item
0 Computer
1 Printer
2 Tablet
3 Desk
4 Chair
<class ‘pandas.core.frame.DataFrame’>
Convert Multiple Pandas Series To Dataframe
In the following data, you will see how we convert numerous Series into one DataFrame.
item brand price
Computer A 700
Printer B 150
Tablet C 300
Desk D 450
Chair E 200
Let’s now build the three Series using the information presented above:
import pandas as pd
item = [‘Computer’, ‘Printer’, ‘Tablet’, ‘Desk’, ‘Chair’]
series_item = pd.Series(item)
print (series_item)
brand = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’]
series_brand = pd.Series(brand)
print(series_brand)
price = [700, 150, 300, 450, 200]
series_price = pd.Series(price)
print(series_price)
Execute this code to obtain the subsequent three Series:
0 Computer
1 Printer
2 Tablet
3 Desk
4 Chair
dtype: object
0 A
1 B
2 C
3 D
4 E
dtype: object
0 700
1 150
2 300
3 450
4 200
dtype: int64
You must do as following to transform the three Series into one DataFrame:
- Three Series to three DataFrames conversion
- Concatenate the three DataFrames into one DataFrame
The full code is provided below:
import pandas as pd
item = [‘Computer’, ‘Printer’, ‘Tablet’, ‘Desk’, ‘Chair’]
series_item = pd.Series(item)
brand = [‘A’, ‘B’, ‘C’, ‘D’, ‘E’]
series_brand = pd.Series(brand)
price = [700, 150, 300, 450, 200]
series_price = pd.Series(price)
df_item = pd.DataFrame(series_item)
df_item = df_item.rename(columns = {0:’item’})
df_brand = pd.DataFrame(series_brand)
df_brand = df_brand.rename(columns = {0:’brand’})
df_price = pd.DataFrame(series_price)
df_price = df_price.rename(columns = {0:’price’})
df_all = pd.concat([df_item, df_brand, df_price], axis=1)
print(df_all)
print(type(df_all))
After the code is executed, you will see this DataFrame:
item brand price
0 Computer A 700
1 Printer B 150
2 Tablet C 300
3 Desk D 450
4 Chair E 200
<class ‘pandas.core.frame.DataFrame’>
Conclusion
Now you know how to convert Pandas Series to DataFrame. Hopefully, you find this tutorial with examples useful and easy to understand. What are you waiting for? Follow along, keep practicing, and you will soon become an expert at this method.Once you master this technique, you can continue to gain more skills related to DataFrame in Pandas. We also provide other tutorials on this subject, such as converting DataFrame to dictionary or sorting Pandas DataFrame; let’s check them out now.
Leave a comment