You can convert Pandas DataFrame to Series using squeeze:
df.squeeze()
In this guide, you’ll see 3 scenarios of converting:
- Single DataFrame column into a Series (from a single-column DataFrame)
- Specific DataFrame column into a Series (from a multi-column DataFrame)
- 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 = {'First_Name': ['Jeff','Tina','Ben','Maria','Rob']} df = pd.DataFrame(data, columns = ['First_Name']) 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):
You can then use df.squeeze() to convert the DataFrame into Series:
import pandas as pd data = {'First_Name': ['Jeff','Tina','Ben','Maria','Rob']} df = pd.DataFrame(data, columns = ['First_Name']) my_series = df.squeeze() print(my_series) print (type(my_series))
The DataFrame will now get converted into a 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 = {'First_Name': ['Jeff','Tina','Ben','Maria','Rob'], 'Last_Name':['Miller','Smith','Lee','Green','Carter'], 'Age':[33,42,29,28,57] } df = pd.DataFrame(data, columns = ['First_Name','Last_Name','Age']) print(df) print (type(df))
Run the code, and you’ll get a DataFrame with 3 columns:
Let’s say that your goal is to convert the ‘Last_Name‘ column into a Series. In that case, you’ll need to add the following syntax to the code:
my_series = df['Last_Name'].squeeze()
So the complete code to perform the conversion is as follows:
import pandas as pd data = {'First_Name': ['Jeff','Tina','Ben','Maria','Rob'], 'Last_Name':['Miller','Smith','Lee','Green','Carter'], 'Age':[33,42,29,28,57] } df = pd.DataFrame(data, columns = ['First_Name','Last_Name','Age']) my_series = df['Last_Name'].squeeze() print(my_series) print (type(my_series))
The ‘Last_Name’ column will now become a 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 ‘Maria Green’ (where the associated index value is 3):
import pandas as pd data = {'First_Name': ['Jeff','Tina','Ben','Maria','Rob'], 'Last_Name':['Miller','Smith','Lee','Green','Carter'], 'Age':[33,42,29,28,57] } df = pd.DataFrame(data, columns = ['First_Name','Last_Name','Age']) my_series = df.iloc[3].squeeze() print(my_series) print (type(my_series))
You’ll get the following Series:
And if you’d like reset the index (to contain only integers), you may use this syntax:
import pandas as pd data = {'First_Name': ['Jeff','Tina','Ben','Maria','Rob'], 'Last_Name':['Miller','Smith','Lee','Green','Carter'], 'Age':[33,42,29,28,57] } df = pd.DataFrame(data, columns = ['First_Name','Last_Name','Age']) 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:
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.