We can set conditions and fetch DataFrame rows. These conditions can be set using logical operators and even relational operators.
At first, import the required pandas libraries −
import pandas as pd
Let us create a DataFrame and read our CSV file −
dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\SalesRecords.csv")
Fetching dataframe rows with registration price less than 1000. We are using relational operator for this −
dataFrame[dataFrame.Reg_Price < 1000]
Example
Following is the code −
import pandas as pd # reading csv file dataFrame = pd.read_csv("C:\\Users\\amit_\\Desktop\\SalesRecords.csv") print("DataFrame...\n",dataFrame) # count the rows and columns in a DataFrame print("\nNumber of rows and column in our DataFrame = ",dataFrame.shape) # fetching dataframe rows with registration price less than 1000 resData = dataFrame[dataFrame.Reg_Price < 1000] print("DataFrame...\n",resData)
Output
This will produce the following output −
DataFrame... Car Date_of_Purchase Reg_Price 0 BMW 10/10/2020 1000 1 Lexus 10/12/2020 750 2 Audi 10/17/2020 750 3 Jaguar 10/16/2020 1500 4 Mustang 10/19/2020 1100 5 Lamborghini 10/22/2020 1000 Number of rows and column in our DataFrame = (6, 3) DataFrame... Car Date_of_Purchase Reg_Price 1 Lexus 10/12/2020 750 2 Audi 10/17/2020 750