How to Convert Pandas DataFrame to a Series?

In Python, Pandas DataFrame is converted into a series to access single columns or rows and to reduce memory usage of data. In Python, some functions, such as value_counts(), first_valid_index(), etc., are only available to a series of objects, so Pandas DataFrame is also converted into a series to use those functions.

Python provides multiple functions to convert the Pandas DataFrame into series, such as df.squeeze(), df.iloc(), etc. 

This post provides an overview of how to convert Pandas DataFrame to a series in Python:

Method 1: Using df.squeeze() Function

The “df.squeeze()” function converts a single column or a single row DataFrame into a Series. This function can also convert a Series or DataFrame with a single element into a scalar value. 

df.squeeze(axis=None)

The “df.squeeze()” function accepts an optional “axis” parameter that specifies which axis to squeeze.

Let’s see different examples to convert single, specific, and multiple Pandas DataFrame to a series:

Example 1: Single DataFrame Column Into a Series

The below code is used to convert the single DataFrame column into series:

Code:

import pandas
data_value = {'Students': ['ALex', 'Joseph', 'Lily', 'Anna']}
data_frame = pandas.DataFrame(data_value, columns = ['Students'])
output = data_frame.squeeze()
print(output, '\n')
print(type(output))
  • The “pd.DataFrame()” function of the Pandas module is used to create the DataFrame by taking the input dictionary and column name as an argument.
  • The “data_frame.squeeze()” function converts the pandas DataFrame to a series.

Output:

The Pandas data frame has been converted into a series.

Example 2: Specific DataFrame Column into a Series

The following code is used to convert the specific DataFrame column into a series:

Code: 

import pandas
data_value = {'Students': ['ALex', 'Joseph', 'Lily', 'Anna'],
              'Roll_No': [45, 47, 49, 52]}
data_frame = pandas.DataFrame(data_value, columns = ['Students', 'Roll_No'])
output = data_frame['Roll_No'].squeeze()
print(output, '\n')
print(type(output))

The “pd.DataFrame()” function creates the DataFrame and the “df.squeeze()” function converts the specific columns of DataFrame into series.

Output:

The specific column has been converted into a series.

Example 3: Single Row in the DataFrame Into a Series

The given below code is used to convert the single specified row into a series:

Code: 

import pandas
data_value = {'Students': ['ALex', 'Joseph', 'Lily', 'Anna'],
              'Roll_No': [45, 47, 49, 52], 'Marks': [95, 78, 45, 65]}
data_frame = pandas.DataFrame(data_value, columns = ['Students', 'Roll_No', 'Marks'])
print(data_frame, '\n')
output = data_frame.iloc[2].reset_index(drop=True).squeeze()
print(output, '\n')
print(type(output))
  • The Pandas module function “pd.DataFrame()” creates the DataFrame of the given data.
  • The “iloc()” function is used to get the third row of the data frame and remove its original index using the “reset_index()” function.
  • Lastly, the “df.squeeze()” function is used to return a single value if possible.

Output:

The specified row has been converted into a series.

Method 2: Using df.iloc() Function

To select a specific row or column from Pandas DataFrame, the “df.iloc()” function is used in Python. The syntax of the Python “df.iloc()” function is shown below:

df.iloc[row_index, column_index]

From the above syntax the parameter “row_index” and parameter “column_index” specify the integers, lists of integers, slices, or boolean arrays.

Example 1: Single DataFrame Column into a Series

The below code is used to convert the single DataFrame column into a series:

Code: 

import pandas
data_value = {'Students': ['ALex', 'Joseph', 'Lily', 'Anna']}
data_frame = pandas.DataFrame(data_value, columns = ['Students'])
output = data_frame.iloc[:,0]
print(output, '\n')
print(type(output))

The “df.iloc[ ]” function takes the index value of the single DataFrame column and converts them into series.

Output: 

The specific single DataFrame column has been converted into the index.

Example 2: Multiple DataFrame Columns Into a Series

The below code is used to convert the specific multiple DataFrame column into series:

Code:

import pandas
data_value = {'Students': ['ALex', 'Joseph', 'Lily', 'Anna'],
              'Age': [45, 22, 18, 16]}
data_frame = pandas.DataFrame(data_value, columns = ['Students', 'Age'])
output = data_frame.iloc[:,0]
output1 = data_frame.iloc[:,1]
print(output)
print(type(output))
print('\n', output1)
print(type(output1))

The “df.iloc[ ]” function takes the index value of the multiple DataFrame columns and converts them into series.

Output:

The specific multiple columns have been converted into series.

Method 3: Using stack() Method

The stack() method can also convert a Pandas DataFrame to a Series. The stack() method reshapes the DataFrame into a table with a new innermost level of rows for each column. The below code is used to convert Pandas DataFrame to a series:

Code:

import pandas
data_frame = pandas.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
print(data_frame)
print(type(data_frame), '\n')
series_value = data_frame.stack()
print(series_value)
print(type(series_value))

The DataFrame containing two columns is created, and the “df.stack()” function is used to convert the complete dataframe into a series.

Output:

The complete dataframe has been converted into a series.

Conclusion

To convert the Pandas DataFrame specific row and columns to a series, the “df.squeeze()” function and the “df.iloc()” function, and “df.stack()” are used in Python. The “df.squeeze()” function converts a single column or row DataFrame into a Series. While the “df.iloc()” function uses a specific index of DataFrame to convert the specific columns and rows into series. 

This guide presented an in-depth guide on how to convert Pandas DataFrame to a series using various examples.