4. Filtering in Pandas
Display only customer ID, name, email, phone, and address columns of the top 3 rows.
Solution
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 CanyonDisplay the last 5 rows and first 5 columns of the dataset.
Solution
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 9518926645Display the last 7 rows and last 7 columns.
Solution
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 ClocksRetrieve the first 3 rows
Solution
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 equipmentDisplay the rows where the order status is shipped.
Display the data of Berlin state.
Display the data for the electronics and clothing category only.
Display the data of Germany where ratings are between 3-5(inclusive).
Display the data of Adidas and Nike with excellent feedback.
Display orders shipped to Canada with a rating of 3 or higher.
Find orders where the total amount is greater than $1,000, the payment method is "Credit Card," and the product category is "Electronics."
Find the companies that have excellent feedback for electronics in the American market.
Create a sample of 50 rows from the above dataset.
Display the first 10 rows of the last 10 columns.
Display the sale records of Polk County but the sale value must be above 500.
Find out the sales in Linn county but the bottle volume must be more than 1000ml.
Display sales records where "Bottle Volume (ml)" is either 500 or 1000, and the "Sale (Dollars)" is over $700.
Show the sales of happened on 10-10-2012 and 11/26/2013 only.
Last updated