4. Filtering in Pandas

  1. Display only customer ID, name, email, phone, and address columns of the top 3 rows.

chevron-rightSolutionhashtag
import pandas as pd
df=pd.read_csv('retail_data.csv')
df.iloc[0:3,1:6]
# this can also be done by df.head(3)[['Customer_ID', 'Name', 'Email', 'Phone', 'Address']]

# Output
    Customer_ID	Name	Email	Phone	Address
0	37249	Michelle Harrington	Ebony39@gmail.com	1414786801	3959 Amanda Burgs
1	69749	Kelsey Hill	Mark36@gmail.com	6852899987	82072 Dawn Centers
2	30192	Scott Jensen	Shane85@gmail.com	8362160449	4133 Young Canyon
  1. Display the last 5 rows and first 5 columns of the dataset.

chevron-rightSolutionhashtag
df.iloc[-5:,0:5]

# Output

        Transaction_ID	Customer_ID	Name	Email	Phone
293906	4246475	12104	Meagan Ellis	Courtney60@gmail.com	7466353743
293907	1197603	69772	Mathew Beck	Jennifer71@gmail.com	5754304957
293908	7743242	28449	Daniel Lee	Christopher100@gmail.com	9382530370
293909	9301950	45477	Patrick Wilson	Rebecca65@gmail.com	9373222023
293910	2882826	53626	Dustin Merritt	William14@gmail.com	9518926645
  1. Display the last 7 rows and last 7 columns.

chevron-rightSolutionhashtag
df.iloc[-7:,-7:]

# Output

Product_Type	Feedback	Shipping_Method	Payment_Method	Order_Status	Ratings	products
293904	Tablet	Average	Same-Day	Cash	Pending	2	Amazon Fire Tablet
293905	Shorts	Excellent	Standard	Cash	Delivered	4	Chino shorts
293906	Fiction	Bad	Same-Day	Cash	Processing	1	Historical fiction
293907	Laptop	Excellent	Same-Day	Cash	Processing	5	LG Gram
293908	Jacket	Average	Express	Cash	Shipped	2	Parka
293909	Furniture	Good	Standard	Cash	Shipped	4	TV stand
293910	Decorations	Average	Same-Day	Cash	Shipped	2	Clocks
  1. Retrieve the first 3 rows

chevron-rightSolutionhashtag
df.iloc[:3]

# Output

Transaction_ID	Customer_ID	Name	Email	Phone	Address	City	State	Zipcode	Country	...	Total_Amount	Product_Category	Product_Brand	Product_Type	Feedback	Shipping_Method	Payment_Method	Order_Status	Ratings	products
0	8691788	37249	Michelle Harrington	Ebony39@gmail.com	1414786801	3959 Amanda Burgs	Dortmund	Berlin	77985	Germany	...	324.086270	Clothing	Nike	Shorts	Excellent	Same-Day	Debit Card	Shipped	5	Cycling shorts
1	2174773	69749	Kelsey Hill	Mark36@gmail.com	6852899987	82072 Dawn Centers	Nottingham	England	99071	UK	...	806.707815	Electronics	Samsung	Tablet	Excellent	Standard	Credit Card	Processing	4	Lenovo Tab
2	6679610	30192	Scott Jensen	Shane85@gmail.com	8362160449	4133 Young Canyon	Geelong	New South Wales	75929	Australia	...	1063.432799	Books	Penguin Books	Children's	Average	Same-Day	Credit Card	Processing	2	Sports equipment
  1. Display the rows where the order status is shipped.

chevron-rightSolutionhashtag
  1. Display the data of Berlin state.

chevron-rightSolutionhashtag
  1. Display the data for the electronics and clothing category only.

chevron-rightSolutionhashtag
  1. Display the data of Germany where ratings are between 3-5(inclusive).

chevron-rightSolutionhashtag
  1. Display the data of Adidas and Nike with excellent feedback.

chevron-rightSolutionhashtag
  1. Display orders shipped to Canada with a rating of 3 or higher.

chevron-rightSolutionhashtag
  1. Find orders where the total amount is greater than $1,000, the payment method is "Credit Card," and the product category is "Electronics."

chevron-rightSolutionhashtag
  1. Find the companies that have excellent feedback for electronics in the American market.

chevron-rightSolutionhashtag
  1. Create a sample of 50 rows from the above dataset.

chevron-rightSolutionhashtag
  1. Display the first 10 rows of the last 10 columns.

chevron-rightSolutionhashtag
  1. Display the sale records of Polk County but the sale value must be above 500.

chevron-rightSolutionhashtag
  1. Find out the sales in Linn county but the bottle volume must be more than 1000ml.

chevron-rightSolutionhashtag

  1. Display sales records where "Bottle Volume (ml)" is either 500 or 1000, and the "Sale (Dollars)" is over $700.

chevron-rightSolutionhashtag
  1. Show the sales of happened on 10-10-2012 and 11/26/2013 only.

chevron-rightSolutionhashtag

Last updated