Q.Use the DataFrame created in Question 9 above to do the following:
k) Write the values of DataFrame Sales to a comma separated file SalesFigures.csv on the disk. Do not write the row labels and column labels.
l) Read the data in the file SalesFigures.csv into a DataFrame SalesRetrieved and Display it. Now update the row labels and column labels of SalesRetrieved to be the same as that of Sales.
You're viewing a preview — the full solution, concept, methods & PYQ mapping are locked.
Start your 14-day free trial to unlock the full solution →Chain a sequence of DataFrame operations — join, transpose, slice, add a row, delete a column and a row, rename labels, update a cell, and round-trip through a CSV file — on the Sales DataFrame from Question 9.
a) Combine Sales and Sales2. Section 2.3.5 taught DataFrame.append(), but append() stacks the SECOND DataFrame's rows below the first, taking the union of columns — since Sales2 has a different column (2018) instead of matching rows, appending it that way would produce 10 rows full of NaN, not a clean fifth column. Because every later part of this question (d, j) needs each person's individual 2018 figure, Sales2 is joined here as a new COLUMN using pd.concat([Sales, Sales2], axis=1), giving a single 5-row × 5-column DataFrame with 2014–2018.
b) Transpose — Sales.T swaps rows and columns: the years become row labels and the sales-person names become column labels.
c) 2017 sales for everyone — Sales[2017] selects that one column, returned as a Series indexed by sales-person.
d) Madhu & Ankit, 2017 and 2018 — Sales.loc[['Madhu', 'Ankit'], [2017, 2018]] selects that 2×2 block by label.
e) Shruti's 2016 sales — Sales.loc['Shruti', 2016] selects a single cell: 125000.
f) Add Sumeet — Sales.loc['Sumeet'] = [...] adds a new row; assigning to a label that doesn't yet exist in the index appends it as a new row (this is how the textbook's own running example adds rows to a DataFrame).
g) Delete the 2014 column — Sales.drop(columns=[2014]).
h) Delete Kinshuk's row — Sales.drop(index=['Kinshuk']).
i) Rename labels — Sales.rename(index={'Ankit': 'Vivaan', 'Madhu': 'Shailesh'}) relabels those two rows; every other row label is unchanged.
j) Update a single value — Sales.loc['Shailesh', 2018] = 100000 overwrites just that one cell.
k) Export — Sales.to_csv('SalesFigures.csv', header=False, index=False) writes only the raw values, one row per line, comma-separated — no header row and no row-label column.
l) Re-import — since the file has no header, pd.read_csv(..., header=None) reads it back with default integer row/column labels; assigning SalesRetrieved.columns = Sales.columns and SalesRetrieved.index = Sales.index restores the original labels.
# (a) Add 2018 as a new column by joining Sales2 alongside Sales.
# NOTE: DataFrame.append() (as taught in S2.3.5) stacks ROWS, not columns -- appending
# Sales2 that way would create 10 NaN-heavy rows instead of a clean 2018 column. Since
# every later part of this question (d, j) needs a per-person 2018 value, the join here
# is done column-wise with pd.concat(axis=1), which is the operation the rest of the
# exercise actually depends on.
Sales = pd.concat([Sales, Sales2], axis=1)
print(Sales)
print(Sales.T) # b) transpose
print(Sales[2017]) # c) sales in 2017, all persons
print(Sales.loc[['Madhu', 'Ankit'], [2017, 2018]]) # d) Madhu & Ankit, 2017 and 2018
print(Sales.loc['Shruti', 2016]) # e) Shruti's 2016 sales
# f) add salesman Sumeet
Sales.loc['Sumeet'] = [196.2, 37800, 52000, 78438, 38852]
# g) delete the 2014 column
Sales = Sales.drop(columns=[2014])
# h) delete Kinshuk's row
Sales = Sales.drop(index=['Kinshuk'])
# i) rename Ankit -> Vivaan, Madhu -> Shailesh
Sales = Sales.rename(index={'Ankit': 'Vivaan', 'Madhu': 'Shailesh'})
# j) update Shailesh's 2018 sale to 100000
Sales.loc['Shailesh', 2018] = 100000
print(Sales)
# k) write to CSV with no row/column labels
Sales.to_csv('SalesFigures.csv', header=False, index=False) …
Unlock everything free for 14 days
- Full step-by-step solutions
- Concept-first explanations
- Methods, shortcuts & mistakes
- PYQ mapping + timed mock tests
Full access for 14 days. No credit card required.