How to Convert Pandas DataFrame to Series

To convert Pandas DataFrame to Series using squeeze:

df.squeeze()

In this guide, you’ll see 3 scenarios of converting:

  1. Single DataFrame column into a Series (from a single-column DataFrame)
  2. Specific DataFrame column into a Series (from a multi-column DataFrame)
  3. Single row in the DataFrame into a Series

(1) Convert a Single DataFrame Column into a Series

To start with a simple example, let’s create a DataFrame with a single column:

import pandas as pd

data = {"Products": ["Computer", "Printer", "Tablet", "Chair", "Desk"]}

df = pd.DataFrame(data)

print(df)
print(type(df))

Run the code in Python, and you’ll get the following DataFrame (note that print(type(df)) was added at the bottom of the code to demonstrate that we got a DataFrame):

   Products
0  Computer
1   Printer
2    Tablet
3     Chair
4      Desk
<class 'pandas.core.frame.DataFrame'>

You can then use df.squeeze() to convert the DataFrame into a Series:

import pandas as pd

data = {"Products": ["Computer", "Printer", "Tablet", "Chair", "Desk"]}

df = pd.DataFrame(data)

my_series = df.squeeze()

print(my_series)
print(type(my_series))

The resulted Series after the conversion:

0    Computer
1     Printer
2      Tablet
3       Chair
4        Desk
Name: Products, dtype: object
<class 'pandas.core.series.Series'>

(2) Convert a Specific DataFrame Column into a Series

What if you have a DataFrame with multiple columns, and you’d like to convert a specific column into a Series?

For example, suppose that you have the following multi-column DataFrame:

import pandas as pd

data = {"Products": ["Computer", "Printer", "Tablet", "Chair", "Desk"],
        "Brand": ["A", "B", "C", "D", "E"],
        "Price": [750, 200, 300, 150, 400]
        }

df = pd.DataFrame(data)

print(df)
print(type(df))

Run the code, and you’ll get a DataFrame with 3 columns:

   Products  Brand  Price
0  Computer      A    750
1   Printer      B    200
2    Tablet      C    300
3     Chair      D    150
4      Desk      E    400
<class 'pandas.core.frame.DataFrame'>

Let’s say that your goal is to convert the “Price” column into a Series. In that case, you’ll need to add the following syntax to the code:

my_series = df["Price"].squeeze()

So the complete code to perform the conversion is as follows:

import pandas as pd

data = {"Products": ["Computer", "Printer", "Tablet", "Chair", "Desk"],
        "Brand": ["A", "B", "C", "D", "E"],
        "Price": [750, 200, 300, 150, 400]
        }

df = pd.DataFrame(data)

my_series = df["Price"].squeeze()

print(my_series)
print(type(my_series))

The “Price” column will now become a Series:

0    750
1    200
2    300
3    150
4    400
Name: Price, dtype: int64
<class 'pandas.core.series.Series'>

(3) Convert a Single Row in the DataFrame into a Series

In the final scenario, you’ll see how to convert a single row in the DataFrame into a Series.

For instance, you can use the syntax below to convert the row that represents the “Chair” product (where the associated index value is 3):

import pandas as pd

data = {"Products": ["Computer", "Printer", "Tablet", "Chair", "Desk"],
        "Brand": ["A", "B", "C", "D", "E"],
        "Price": [750, 200, 300, 150, 400]
        }

df = pd.DataFrame(data)

my_series = df.iloc[3].squeeze()

print(my_series)
print(type(my_series))

You’ll then get the following Series:

Products    Chair
Brand           D
Price         150
Name: 3, dtype: object
<class 'pandas.core.series.Series'>

And if you’d like reset the index (to contain only integers), you may use this syntax:

import pandas as pd

data = {"Products": ["Computer", "Printer", "Tablet", "Chair", "Desk"],
        "Brand": ["A", "B", "C", "D", "E"],
        "Price": [750, 200, 300, 150, 400]
        }

df = pd.DataFrame(data)

my_series = df.iloc[3].reset_index(drop=True).squeeze()

print(my_series)
print(type(my_series))

Here is the Series with the new index that contains only integers:

0    Chair
1        D
2      150
Name: 3, dtype: object
<class 'pandas.core.series.Series'>

Additional Resources

You may want to check the following guide to learn how to convert Pandas Series into a DataFrame.

The Pandas Documentation also contains additional information about squeeze.

Leave a Comment