3.Use above dataframe and do the following:

 Change the DataFrame Sales such that it becomes its transpose. Display the sales made by all sales persons in the year 2018.

Display the sales made by Kapil and Mohini in the year 2019 and 2020.

Add data to Sales for salesman Nirali where the sales made are

[221, 178, 165, 177, 210] in the years [2018, 2019, 2020, 2021]

respectively

Delete the data for the year 2018 from the DataFrame Sales. Delete the data for sales man Shikhar from the DataFrame Sales.

Change the name of the salesperson Kamini to Rani and Kapil to Anil.

Update the sale made by Mohini in 118 to 150 in 2018

 

Solution :- 

import pandas as pd #Creating DataFrame
d = {2018:[110,130,115,118],
2019:[205,165,175,190],
2020:[115,206,157,179],
2021:[118,198,183,169]}
sales=pd.DataFrame(d,index=[‘Kapil’,’Kamini’,’Shikhar’,’Mohini’])
print(“Transpose:”)
print(“~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~”)
print(sales.T)
print(“\nSales made by each salesman in 2018”)

#Method 1 print(sales[2018])

#Method 2 print(sales.loc[:,2018])
print(“Sales made by Kapil and Mohini:”) #Method 1
print(sales.loc[[‘Kapil’,’Mohini’], [2019,2020]])

#Method 3 print(sales.loc[sales.index.isin([“Kapil”,”Mohini”]),[2019,2020]])

print(“Add Data:”)
sales.loc[“Nirali”]=[221, 178, 165, 177]
print(sales)
print(“Delete Data for 2018:”)
sales=sales.drop(columns=2018)
print(sales)

Sales.drop(columns=2018,inplace=True)
print(Sales)
sales=sales.drop(“Shikhar”,axis=0) #sales.drop(“kinshuk”)
print(sales)
sales=sales.rename({“Kamini”:”Rani”,”Kapil”:”Anil”},axis=”inde x”)
print(sales)
sales.loc[sales.index==”Mohini”,2018]=150
print(sales)