pandas.DataFrame.loc
es una función utilizada para seleccionar filas de Pandas DataFrame según la condición proporcionada. En este artículo, aprendamos a seleccionar las filas de Pandas DataFrame según algunas condiciones.
Sintaxis: df.loc[df[‘cname’] ‘condición’]
Parámetros:
df: representa el marco de datos
cname: representa el nombre de la columna
condition: representa la condición en la que se deben seleccionar las filas
Ejemplo 1:
# Importing pandas as pd from pandas import DataFrame # Creating a data frame cart = {'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 frame print("Original data frame:\n") print(df) # Selecting the product of Electronic Type select_prod = df.loc[df['Type'] == 'Electronic'] print("\n") # Print selected rows based on the condition print("Selecting rows:\n") print (select_prod)
Salida:
Ejemplo 2:
# Importing pandas as pd from pandas import DataFrame # Creating a data frame cart = {'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 frame print("Original data frame:\n") print(df) # Selecting the product of HomeAppliances Type select_prod = df.loc[df['Type'] == 'HomeAppliances'] print("\n") # Print selected rows based on the condition print("Selecting rows:\n") print (select_prod)
Salida:
Ejemplo 3:
# Importing pandas as pd from pandas import DataFrame # Creating a data frame cart = {'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 frame print("Original data frame:\n") print(df) # Selecting the product of Price greater # than or equal to 25000 select_prod = df.loc[df['Price'] >= 25000] print("\n") # Print selected rows based on the condition print("Selecting rows:\n") print (select_prod)
Salida:
Ejemplo 4:
# Importing pandas as pd from pandas import DataFrame # Creating a data frame cart = {'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 frame print("Original data frame:\n") print(df) # Selecting the product of Price not # equal to 30000 select_prod = df.loc[df['Price'] != 30000] print("\n") # Print selected rows based on the condition print("Selecting rows:\n") print (select_prod)
Producción:
Publicación traducida automáticamente
Artículo escrito por utkarsh_kumar y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA