• Post author:
  • Post category:Pandas
  • Post last modified:March 27, 2024
  • Reading time:8 mins read
You are currently viewing Convert Pandas Series to DataFrame

You can convert pandas series to DataFrame by using the pandas Series.to_frame()method. This function is used to convert the given series object to a DataFrame. In this article, you can see how to convert the pandas series to DataFrame and also convert multiple series into a DataFrame with several examples.

Advertisements

1. Quick Examples to Convert Series to DataFrame

If you are in hurry below are some quick examples to convert series to DataFrame.


# Below are some quick examples.

# Create a Series.
import pandas as pd
Courses = ['Python', 'PySpark', 'Spark', 'Java', 'Pega']
my_series = pd.Series(Courses)

# Convert Pandas series to DataFrame.
df = my_series.to_frame()

# Converting multiple Series into DataFrame.
df_Courses= pd.DataFrame(series_Courses)
df_Courses = df_Courses.rename(columns = {0:'Courses'})

df_Duration = pd.DataFrame(series_Duration)
df_Duration = df_Duration.rename(columns = {0:'Duration'})

df_Fee = pd.DataFrame(series_Fee)
df_Fee = df_Fee.rename(columns = {0:'Fee'})

df_total = pd.concat([df_Courses, df_Duration, df_Fee ], axis=1)   

2. Pandas Series.to_frame() Syntax

Following is the syntax of the Series.to_frame() function.


# Syntax of Pandas series.to_frame() function.
Series.to_frame(name=None)

Parameter name: The passed name should substitute for the series name (if it has one).
Returns: DataFrame

Let’s create a Series. pandas Series is a one-dimensional labeled array capable of holding any data type. A pandas Series can be created out of a Python list or NumPy array. Note that, unlike Python lists, a Series will always contain data of the same type. This makes NumPy array a better candidate for creating a pandas series.


# Create a Series.
import pandas as pd
Courses = ['Python', 'PySpark', 'Spark', 'Java', 'Pega']
my_series = pd.Series(Courses)
print(my_series)
print(type(my_series))

Yields below output.


# Output:
0     Python
1    PySpark
2      Spark
3       Java
4       Pega
dtype: object

3. Convert Pandas Series to DataFrame

You can convert pandas series to DataFrame by using Series.to_frame() function. A DataFrame is nothing but a collection of one or more Series (1+). We can generate the DataFrame by using a Single Series or by combining multiple Series.


# Convert Pandas series to DataFrame.
my_series = pd.Series(Courses)
df = my_series.to_frame(1)
print(df)

Yields below output.


# Output:
         1
0   Python
1  PySpark
2    Spark
3     Java
4     Pega

NOTE: The column name is ‘0’. Alternatively, you can rename the column by using DataFrame.rename() function by following the below example.


# Rename the column of DataFrame.
df = my_series.to_frame()
df = df.rename(columns = {0:'Courses'})
print(df)

Yields below output.


# Output:
   Courses
0   Python
1  PySpark
2    Spark
3     Java
4     Pega

4. Multiple Series to DataFrame

In this section, you can find out how to turn multiple series to DataFrame by the following example. In this first, we have taken three Series, make them into DataFrame, and then into one big DataFrame.


# Create Three Series.
Courses = ['Python', 'PySpark', 'Spark', 'Java', 'Pega']
series_Courses = pd.Series(Courses)

Duration = ['30days', '35days', '50days', '60days', '45days']
series_Duration = pd.Series(Duration)

Fee = [10000, 15000, 30000, 35000, 20000]
series_Fee = pd.Series(Fee)

Convert 3 Series into 3 DataFrames and Concatenate the 3 DataFrames into a single DataFrame by using the pandas.concat() method. While creating DataFrame, I have assigned column names to it.


# Converting Series into DataFrames
df_Courses= pd.DataFrame(series_Courses)
df_Courses = df_Courses.rename(columns = {0:'Courses'})

df_Duration = pd.DataFrame(series_Duration)
df_Duration = df_Duration.rename(columns = {0:'Duration'})

df_Fee = pd.DataFrame(series_Fee)
df_Fee = df_Fee.rename(columns = {0:'Fee'})

df_total = pd.concat([df_Courses, df_Duration, df_Fee ], axis=1)
print(df_total)

Yields below output.


# Output:
   Courses Duration    Fee
0   Python   30days  10000
1  PySpark   35days  15000
2    Spark   50days  30000
3     Java   60days  35000
4     Pega   45days  20000

5. Complete Examples of Convert Series into DataFrame


# Below are complete examples.
# Syntax of Pandas series.to_frame() function.
Series.to_frame(name=None)
Parameter name : The passed name should substitute for the series name (if it has one).
Returns : data_frame : DataFrame

# Create a Series.
import pandas as pd
Courses = ['Python', 'PySpark', 'Spark', 'Java', 'Pega']
my_series = pd.Series(Courses)
print(my_series)
print(type(my_series))

# Convert Pandas series to DataFrame.
my_series = pd.Series(Courses)
df = my_series.to_frame(1)

# Converting # series into 3 DataFrames.
df_Courses= pd.DataFrame(series_Courses)
df_Courses = df_Courses.rename(columns = {0:'Courses'})

df_Duration = pd.DataFrame(series_Duration)
df_Duration = df_Duration.rename(columns = {0:'Duration'})

df_Fee = pd.DataFrame(series_Fee)
df_Fee = df_Fee.rename(columns = {0:'Fee'})

df_total = pd.concat([df_Courses, df_Duration, df_Fee ], axis=1)

Conclusion

In this article, You can see how to convert series to DataFrame by creating a series, converting single series to DataFrame, Converting multiple series to DataFrame.

References