Как выбрать строки из Pandas DataFrame?
pandas.DataFrame.loc is a function used to select rows from Pandas DataFrame based on the condition provided. In this article, let’s learn to select the rows from Pandas DataFrame based on some conditions.
Syntax: df.loc[df[‘cname’] ‘condition’]
Parameters:
df: represents data frame
cname: represents column name
condition: represents condition on which rows has to be selected
Example 1:
# Importing pandas as pdfrom pandas import DataFrame # Creating a data framecart = {"Product": ["Mobile", "AC", "Laptop", "TV", "Football"], "Type": ["Electronic", "HomeAppliances", "Electronic", "HomeAppliances", "Sports"], "Price": [10000, 35000, 50000, 30000, 799] } df = DataFrame(cart, columns = ["Product", "Type", "Price"]) # Print original data frameprint("Original data frame:
")print(df) # Selecting the product of Electronic Typeselect_prod = df.loc[df["Type"] == "Electronic"] print("
") # Print selected rows based on the conditionprint("Selecting rows:
")print (select_prod) |
Выход: 
Пример 2:
# Importing pandas as pdfrom pandas import DataFrame # Creating a data framecart = {"Product": ["Mobile", "AC", "Laptop", "TV", "Football"], "Type": ["Electronic", "HomeAppliances", "Electronic", "HomeAppliances", "Sports"], "Price": [10000, 35000, 50000, 30000, 799] } df = DataFrame(cart, columns = ["Product", "Type", "Price"]) # Print original data frameprint("Original data frame:
")print(df) # Selecting the product of HomeAppliances Typeselect_prod = df.loc[df["Type"] == "HomeAppliances"] print("
") # Print selected rows based on the conditionprint("Selecting rows:
")print (select_prod) |
Output:
Example 3:
# Importing pandas as pdfrom pandas import DataFrame # Creating a data framecart = {"Product": ["Mobile", "AC", "Laptop", "TV", "Football"], "Type": ["Electronic", "HomeAppliances", "Electronic", "HomeAppliances", "Sports"], "Price": [10000, 35000, 50000, 30000, 799] } df = DataFrame(cart, columns = ["Product", "Type", "Price"]) # Print original data frameprint("Original data frame:
")print(df) # Selecting the product of Price greater # than or equal to 25000select_prod = df.loc[df["Price"] >= 25000] print("
") # Print selected rows based on the conditionprint("Selecting rows:
")print (select_prod) |
Output:
Example 4:
# Importing pandas as pdfrom pandas import DataFrame # Creating a data framecart = {"Product": ["Mobile", "AC", "Laptop", "TV", "Football"], "Type": ["Electronic", "HomeAppliances", "Electronic", "HomeAppliances", "Sports"], "Price": [10000, 35000, 30000, 30000, 799] } df = DataFrame(cart, columns = ["Product", "Type", "Price"]) # Print original data frameprint("Original data frame:
")print(df) # Selecting the product of Price not # equal to 30000select_prod = df.loc[df["Price"] != 30000] print("
") # Print selected rows based on the conditionprint("Selecting rows:
")print (select_prod) |
Выход: 
Внимание компьютерщик! Укрепите свои основы с помощью базового курса программирования Python и изучите основы.
Для начала подготовьтесь к собеседованию. Расширьте свои концепции структур данных с помощью курса Python DS. А чтобы начать свое путешествие по машинному обучению, присоединяйтесь к курсу Машинное обучение - базовый уровень.