• Post author:
  • Post category:Pandas
  • Post last modified:December 4, 2024
  • Reading time:12 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

Key Points –

  • A Pandas Series can be easily converted to a DataFrame using the to_frame() method.
  • The to_frame() method allows specifying the axis for the resulting DataFrame, either as a single-column DataFrame or as a row.
  • When converting, the Series index can become the DataFrame’s index, and the Series name can be set as the column name.
  • Multiple Series can be combined into a DataFrame using the pd.concat() function.
  • Converting a Series to a DataFrame is generally efficient in terms of performance and memory usage.
  • Series containing lists or other complex data types can be expanded into multiple columns when converted to a DataFrame.

Quick Examples to Convert Series to DataFrame

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


# Quick examples to convert series to DataFrame

# 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)   

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

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

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

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)

FAQ on Convert Pandas Series to DataFrame

How can I convert a Pandas Series to a DataFrame?

To convert a Pandas Series to a DataFrame, you can use the pd.DataFrame() constructor. By default, the Series will be turned into a single-column DataFrame.

How can I specify a column name when converting a Series to a DataFrame?

You can specify the column name by passing a list of column names as the second argument to pd.DataFrame().

Can I convert multiple Series to a DataFrame?

You can convert multiple Series into a DataFrame by passing them into a dictionary or by passing each Series as a separate argument.

Can I convert a Series to a DataFrame with multiple columns?

You can convert a Series into a DataFrame with multiple columns by using pd.concat() or by reshaping the Series.

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