Professional Documents
Culture Documents
[4] Given a Series, print all the elements that are above the 75th
percentile.
Solution:
import pandas as pd
def Ser_stumarks():
std_marks = []
for i in range(1,6):
m = int(input("Enter the Percentile:"))
std_marks.append(m)
s = pd.Series(index=range(1201,1206),data=std_marks)
print("Data fetched from the series are:")
print(s[s>=75])
Ser_stumarks()
[5] Create a data frame for examination results and
display row labels, column labels data types of each
column and the dimensions.
Solution:
import pandas as pd
def df_std_res():
res={'Amit':[76,78,75,66,68],
'Shialesh':[78,56,77,49,55],
'Rani':[90,91,93,97,99],
'Madan':[55,48,59,60,66],
'Radhika':[78,79,85,88,86]}
df=pd.DataFrame(res)
print("Prinitng row labels in a list:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
idx=df.index
l=list(idx)
print(l)
print("Prinitng row labels in a list:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print("[",end=" ")
for col in df.columns:
print(col,end=" ")
print("]")
print("Printing Data Types of each column")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print(df.dtypes)
print("Printing dimensions of Data Frame")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
print(df.ndim)
df_std_res()
data = [["Virat",55,66,31],["Rohit",88,66,43],[
"Hardik",99,101,68]]
players = pd.DataFrame(data,
columns = ["Name","Match-1","Match-2","Match-3"])
print("Iterating by rows:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
for index, row in players.iterrows():
print(index, row.values)
print("Iterating by columns:")
print("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~")
for index, row in players.iterrows():
print(index, row["Name"],row["Match-1"],
row["Match-2"],row["Match-3"])
[7] Create a dataframe and print it along with their index
using iteritems().
import pandas as pd
def df_operations():
sc_4yrs={2016:{'Virat Kohli':2595,'Rohit Sharma':2406,'Shikhar
Dhawan':2378},
2017:{'Virat Kohli':2818,'Rohit Sharma':2613,'Shikhar
Dhawan':2295},
2018:{'Virat Kohli':2735,'Rohit Sharma':2406,'Shikhar
Dhawan':2378},
2019:{'Virat Kohli':2455,'Rohit Sharma':2310,'Shikhar
Dhawan':1844}}
df=pd.DataFrame(sc_4yrs)
print(df)
print("----------------------------------------------------------------
--------")
for (year,runs) in df.iteritems():
print("Year:",year)
print(runs)
df_operations()
[8] Create the following DataFrame Sales containing year
wise sales figures for five salespersons in INR. Use the
years as column labels, and salesperson names as row
labels.
2018 2019 2020 2021
#Method 1
print("Using tail function:")
print(Sales.tail(2))
#Method 2
print("Using iloc")
print(Sales.iloc[-2:])
#Method 2
print(Sales[Sales.columns[0:2]])
#Method 3
print(Sales.iloc[:, 0:2] )
[10] Use above dataframe and do the following:
1. Change the DataFrame Sales such that it becomes its
transpose.
2. Display the sales made by all sales persons in the year
2018.
3. Display the sales made by Kapil and Mohini in the year
2019 and 2020.
4. 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
5. Delete the data for the year 2018 from the DataFrame
Sales.
6. Delete the data for sales man Shikhar from the
DataFrame Sales.
7. Change the name of the salesperson Kamini to Rani and
Kapil to Anil.
8. 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])
#Method 2
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="index")
print(sales)
sales.loc[sales.index=="Mohini",2018]=150
print(sales)