Python lesson
Picking rows and saving
6 orders, saved to picked.csv. index=False leaves out the row numbers, so the file opens cleanly in Excel.
Try this lesson free →Example
.isin keeps the rows that match any value in a list, and .to_csv saves the result as a file:
picked = df[df["Product"].isin(["Earbuds", "Charger"])]
picked.to_csv("picked.csv", index=False)
pickedOrder Day Store Product Qty Revenue
0 O101 Mon Lagos Earbuds 2 70
1 O102 Mon Abuja Charger 1 15
3 O104 Wed Lagos Earbuds 1 35
4 O105 Wed Abuja Earbuds 4 140
5 O106 Thu Lagos Charger 2 30
7 O108 Fri Lagos Earbuds 3 105Key points
.isin(["Earbuds", "Charger"])Keep rows whose value is in the list.
.str.contains("Ear")Keep rows whose text includes "Ear". Capitals matter.
.loc[test, ["Store", "Revenue"]]Rows by a test, then only the columns you want.
.to_csv("file.csv", index=False)Save it as a file. index=False leaves out the row numbers.
Pick with .isin, .str.contains or .loc, then save with .to_csv("file.csv", index=False).
Practice
1. Build a line that keeps only the Earbuds and Charger orders.
df[df["Product"].isin(["Earbuds", "Charger"])]2. Build a line that shows the Store and Revenue of every order over 50.
df.loc[df["Revenue"] > 50, ["Store", "Revenue"]]3. Show only the Earbuds and Phone case orders.
df[df["Product"].isin(["Earbuds", "Phone case"])]4. Show the orders whose product contains the word case.
df[df["Product"].str.contains("case")]5. Save the Lagos orders as lagos.csv for the store manager, without the row numbers.
lagos = df[df["Store"] == "Lagos"]
lagos.to_csv("lagos.csv", index=False)Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.
Try this lesson free →