Home »
Python »
Python Programs
Python - Get particular row as series from pandas dataframe
Given a Pandas DataFrame, we have to get particular row as series.
By Pranit Sharma Last updated : September 26, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Rows in pandas are the different cell (column) values that are aligned horizontally and also provide uniformity. Each row can have the same or different value. Rows are generally marked with the index number but in pandas, we can also assign index names according to the needs.
A Series in pandas contains a single list that can store heterogeneous types of data, because of this, a series is also considered a 1-dimensional data structure.
Getting particular row as series from pandas dataframe
To select a particular row as a series from pandas DataFrame, we will select a column and use the .squeeze() method. This method selects dataframes with a single column or a single row are squeezed to a series.
Let us understand with the help of an example,
Python program to get particular row as series from pandas dataframe
# Importing pandas package
import pandas as pd
# Creating a dictionary
d = {
'Name':['Rony','Tony','Sony'],
'City':['Surat','Faridabad','Delhi']
}
# Creating a DataFrame
df = pd.DataFrame(d)
# Display DataFrame
print("DataFrame1:\n",df,"\n")
# Get the particular row as a series
result = df[df["City"] == "Surat"].squeeze()
# Display result
print("Result:\n",result)
Output
Python Pandas Programs »