Advertisements
Advertisements
Question
Use the DataFrame created in Question 9 above to do the following:
- Append the DataFrame Sales2 to the DataFrame Sales.
- Change the DataFrame Sales such that it becomes its transpose.
- Display the sales made by all sales persons in the year 2017.
- Display the sales made by Madhu and Ankit in the year 2017 and 2018.
- Display the sales made by Shruti 2016.
- Add data to Sales for salesman Sumeet where the sales made are [196.2, 37800, 52000, 78438, 38852] in the years [2014, 2015, 2016, 2017, 2018] respectively.
- Delete the data for the year 2014 from the DataFrame Sales.
- Delete the data for sales man Kinshuk from the DataFrame Sales.
- Change the name of the salesperson Ankit to Vivaan and Madhu to Shailesh.
- Update the sale made by Shailesh in 2018 to 100000.
- 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.
- 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.
Code Writing
Advertisements
Solution
import pandas as pd
Sales = pd.concat([Sales, Sales2])
Sales = Sales.T
print(Sales[2017])
print(Sales.loc[[2017, 2018], ['Madhu', 'Ankit']])
print(Sales.loc[2016, 'Shruti'])
Sales.loc['Sumeet'] = [196.2, 37800, 52000, 78438, 38852]
Sales.drop(2014, axis=1, inplace=True)
Sales.drop('Kinshuk', axis=1, inplace=True)
Sales.rename(columns={'Ankit':'Vivaan', 'Madhu':'Shailesh'}, inplace=True)
Sales.loc[2018, 'Shailesh'] = 100000
Sales.to_csv("SalesFigures.csv", header=False, index=False)
SalesRetrieved = pd.read_csv("SalesFigures.csv", header=None)
SalesRetrieved.index = Sales.index
SalesRetrieved.columns = Sales.columns
print(SalesRetrieved)shaalaa.com
Is there an error in this question or solution?
