Convert Pandas DataFrame to Series

  • Post author:
  • Post category:Pandas
  • Post last modified:October 5, 2023

We 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 to Convert DataFrame to Series

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


# Below are a quick example

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

# Display type
print(type(ser))

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

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

# Example 4: 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 e.t.c). 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.


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)

Yields below output.


# Output:
   Courses    Fee Duration  Discount
0    Spark  20000   30days      1000
1  PySpark  25000   40days      2300
2   Python  22000   35days      1200
3   Pandas  30000   50days      2000
4   Python  25000   40days      2300

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(ser)

# Display type
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'>

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

You can also use df[] or df.squeeze() function to convert the column of the Pandas DataFrame to a series, In order to do so first select the column you wanted to convert and call the squeeze.


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

# Display type
print(type(ser)

Yields below output.


# Output:
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.


# Using loc[]
ser = df.loc[:,'Courses']
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 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))

8. 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

I am a Data Engineer with 20+ years of experience in transforming data into actionable insights. Over the years, I have honed my expertise in designing, implementing, and maintaining data pipelines with frameworks like Apache Spark, PySpark, Pandas, R, Hive and Machine Learning. My journey in the field of data engineering has been a continuous learning, innovation, and a strong commitment to data integrity. I have started this SparkByExamples.com to share my experiences with the data as I come across. You can learn more about me at LinkedIn
You are currently viewing Convert Pandas DataFrame to Series