How to Convert a pandas DataFrame to a Series
In this tutorial, you will learn how to convert a pandas DataFrame to a Series.
TLDR solution
# if single-column df
df.squeeze()
# or just select a column
df['column']
Example 1: When You Have a Single-Column DataFrame
Use squeeze:
import pandas as pd
data = {'Fish': ['salmon', 'pufferfish', 'shark']}
df = pd.DataFrame(data)
print(type(df.squeeze()))
The output:
<class 'pandas.core.series.Series'>
Example 2: General Solution
Just select the column that you want to have as Series:
import pandas as pd
data = {'Fish': ['salmon', 'pufferfish', 'shark']}
df = pd.DataFrame(data)
print(type(df['Fish']))
The output:
<class 'pandas.core.series.Series'>
That's it! You just learned how to convert a DataFrame to a Series.