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

In Pandas, a DataFrame is a two-dimensional labeled data structure with columns that can be of different data types, while a Series is a one-dimensional labeled array capable of holding any data type. If you want to convert a single column or a subset of columns from a DataFrame to a Series, you can use the indexing notation. You can convert Pandas DataFrame to series by using the df[], DataFrame.iloc[], and squeeze() method. These functions are used to convert the columns or rows of the Pandas DataFrame to series.

In this pandas article, you can see how to get the single or multiple columns of DataFrame as a series and also get rows as a series with several examples.

Related: Convert Series to DataFrame in Pandas

1. Quick Examples of Convert DataFrame to Series

If you are in hurry, below are some quick examples of converting DataFrame to series.


# Quick examples of convert dataframe to series

# Example 1: Convert first column to Series
ser = df.iloc[:,0]

# Display type
print(type(ser))

# Example 2: Convert the 'Courses' column to a Series
ser = df['Courses']

# Example 3: Use squeeze() method
ser = df['Courses'].squeeze()

# Example 4: Convert specific column to Series
ser = df.iloc[:,2]

# Example 5: Convert DataFrame row to Series
ser = df.iloc[2].reset_index(drop=True).squeeze()

Python pandas is widely used for data science/data analysis and machine learning applications. It is built on top of another popular package named NumPy, which provides scientific computing in Python. pandas DataFrame is a 2-dimensional labeled data structure with rows and columns (columns of potentially different types like integers, strings, float, None, Python objects, etc.). You can think of it as an excel spreadsheet or SQL table.

We can create DataFrame in many ways here, I will create pandas DataFrame using Python Dictionary.


# Create DataFrame
import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","Pandas","Python"],
    'Fee' :[20000,25000,22000,30000,25000],
    'Duration':['30days','40days','35days','50days','40days'],
    'Discount':[1000,2300,1200,2000,2300]
              }
df = pd.DataFrame(technologies)
print("Create DataFrame\n",df)

Yields below output.

pandas convert dataframe series

2. Convert DataFrame Column to Series

In pandas, each column is represented as a Series hence it is very easy to convert the values of a DataFrame column to a Series. Use df.iloc[:,0] to get the selected column as a Series. This example converts the first column of the Pandas DataFrame to a series.


# Converting first column to Series
ser = df.iloc[:,0]
print("Converted Series:\n",ser)

# Display type
print(type(ser))

Yields below output.

pandas convert dataframe series

Similarly, converting a DataFrame column to a Series in pandas. You can use the indexing notation with the column name to extract that column as a Series.

In this example, df['Courses'] extracts the ‘Name’ column from the DataFrame, and it is stored as a Series named ser. The resulting Series retains the index from the DataFrame.


# Convert the 'Courses' column to a Series
ser = df['Courses']
print("Converted Series:\n",ser)

# Display type
print(type(ser)

Yields the same output as above.

3. Use squeeze() to Convert DataFrame Column to Series

The squeeze() method in pandas can be used to convert a DataFrame column to a Series. The squeeze() method is designed to simplify the DataFrame when possible by converting a one-column DataFrame to a Series.

In the below example, df['Duration'].squeeze() is used to convert the ‘Duration’ column to a Series. The squeeze() method is particularly useful when you know that the resulting object will be a Series, and you want to simplify the DataFrame to a Series if possible.


# Converting duration column to Series
ser = df['Duration'].squeeze()
print("Converted Series:\n",ser)

# Display type
print(type(ser)

Yields below output.


# Output:
# Converted Series:
 0    30days
1    40days
2    35days
3    50days
4    40days
Name: Duration, dtype: object
<class 'pandas.core.series.Series'>

4. Use loc[] to Get Column of DataFrame as Series

Alternatively, you can also use pandas.DataFrame.loc[] to get the specific DataFrame column as Series. By using this you can specify the column name you wanted to convert to Series.

In the below example, df.loc[:, 'Courses'] extracts the ‘Courses’ column as a Series. The colon (:) before the comma is used to select all rows, and ‘Courses’ specifies the column you want to extract. The resulting ser will have the same index as the original DataFrame.


# Using loc[] function
ser = df.loc[:,'Courses']
print("Converted Series:\n",ser)
print(type(ser))

Yields below output.


# Output:
0      Spark
1    PySpark
2     Python
3     Pandas
4     Python
Name: Courses, dtype: object
<class 'pandas.core.series.Series'>

5. Convert DataFrame Row to Series

Use squeeze() function to convert the single Pandas DataFrame row to series. For instance, df.iloc[2].reset_index(drop=True).squeeze() function converts row 3 into a Series. Alternatively, you can also use loc[] to specify the row by name.


# Convert DataFrame row to Series
ser = df.iloc[2].reset_index(drop=True).squeeze()
print(ser)

# Display type
print (type(ser))

Yields below output.


# Output:
0    Python
1     22000
2    35days
3      1200
Name: 2, dtype: object

6. Complete Example For Convert DataFrame to Series


import pandas as pd
technologies = {
    'Courses':["Spark","PySpark","Python","Pandas","Python"],
    'Fee' :[20000,25000,22000,30000,25000],
    'Duration':['30days','40days','35days','50days','40days'],
    'Discount':[1000,2300,1200,2000,2300]
              }
df = pd.DataFrame(technologies)
print(df)

# Converting first column to Series
ser = df.iloc[:,0]
print(ser)

# Display type
print(type(ser))

# Use squeeze() method
ser = df['Courses'].squeeze()
print(ser)

# Display type
print(type(ser))

# Converting specific column to Series
ser = df.iloc[:,2]
print(ser)

# Display type
print(type(ser))

# Converting duration column to Series
ser = df['Duration'].squeeze()
print(ser)

# Display type
print(type(ser)
      
# Convert multiple columns of DataFrame to Series
ser1 = df.iloc[:,1]
print(ser1)

ser2 = df.iloc[:,2]
print(ser2)

ser3 = df.iloc[:,3]
print(ser3)

# Convert DataFrame row to Series
ser = df.iloc[2].reset_index(drop=True).squeeze()
print(ser)

# Display type
print (type(ser))

Frequently Asked Questions on Convert Pandas DataFrame to Series

What is the difference between a DataFrame and a Series in Pandas?

In Pandas, a DataFrame is a two-dimensional tabular data structure with labeled axes (rows and columns), while a Series is a one-dimensional labeled array. A DataFrame can be thought of as a container for multiple Series, where each column is a Series.

How can I convert a single column of a DataFrame to a Series?

Converting a single column of a DataFrame to a Series is straightforward. You can use the indexing notation with the column name to extract that column as a Series.

Can I convert multiple columns of a DataFrame to Series?

You can convert multiple columns of a DataFrame to Series. Use the iloc or loc accessor to select the desired columns, and each extracted column will be a Series.

How can I convert a DataFrame to a Series if I have a specific row selected?

If you want to convert a specific row of a DataFrame to a Series, you can use the iloc or loc accessor to select the row, and the resulting object will be a Series.

Are there any performance considerations when converting DataFrame columns to Series?

Converting DataFrame columns to Series is a lightweight operation, and it is generally efficient. However, keep in mind that both DataFrames and Series are mutable objects, so modifying a Series may affect the original DataFrame.

Will the index be preserved when converting a DataFrame column to a Series?

When you convert a DataFrame column to a Series, the index is preserved. The resulting Series will inherit the index from the DataFrame column.

Conclusion

In this article, you have learned how to convert DataFrame to series by creating a DataFrame, converting a single row or column to series, and converting multiple rows/columns of DataFrame to series.

Happy Learning!!

References

Naveen Nelamali

Naveen Nelamali (NNK) is a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, He has honed his expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. Naveen journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. In this blog, he shares his experiences with the data as he come across. Follow Naveen @ LinkedIn and Medium