Python practice: Column Operations, Math & Type Casting
Chapter 22 · 8 questions · included in full access
Question 1 · easy
Calculate gross revenue column
Solve it in your browserLoad
"sales.csv". Createsales["CalcRev"] = sales["Units"] * sales["Price"]. Printint(sales["CalcRev"].sum()):Question 2 · easy
Calculate 18% GST tax column
Solve it in your browserLoad
"sales.csv". Createsales["Tax"] = (sales["Revenue"] * 0.18).round(2). Printround(sales["Tax"].sum(), 2):Question 3 · easy
Cast column type with astype
Solve it in your browserGiven
df = pd.DataFrame({"ID": ["1", "2"], "Qty": ["10", "20"]}), castdf["Qty"] = df["Qty"].astype(int)and printdf["Qty"].sum():Question 4 · easy
Map territory directors with .map
Solve it in your browserLoad
"sales.csv". Map directors:heads = {"North": "Arjun", "South": "Kavita", "East": "Debashis", "West": "Meera"}. Createsales["Director"] = sales["Region"].map(heads). Printsales["Director"].iloc[0]:Question 5 · medium
Apply custom row classification function
Solve it in your browserLoad
"sales.csv". Apply functionlambda r: "High" if r["Revenue"] >= 30000 else "Standard"withaxis=1intosales["Tier"]. Printsales["Tier"].value_counts()["High"]:Question 6 · easy
Rename column headers
Solve it in your browserLoad
"sales.csv". Rename columns usingsales = sales.rename(columns={"OrderID": "InvoiceNo", "Rep": "Agent"}). Printlist(sales.columns)[0]on line 1 andlist(sales.columns)[3]on line 2:Question 7 · easy
Drop unnecessary columns
Solve it in your browserLoad
"sales.csv". Drop the"Category"column usingclean = sales.drop(columns=["Category"]). Printlen(clean.columns):Question 8 · medium
Rank transactions by revenue
Solve it in your browserLoad
"sales.csv". Compute rankingsales["RevRank"] = sales["Revenue"].rank(ascending=False, method="min").astype(int). Printsales["RevRank"].min():
These questions are part of full access.
The first chapters of every track are free. Full access is a one-time payment.
See pricing