9. Assume that required libraries (pandas and numpy) are imported and dataframe df2 has been created as per questions 4 and 5 above. Predict the output produced by following code fragment :

print(df2.loc[“Jiya”])
print(df2.loc [“Jiya”,”IQ”])
print(df2.loc[“Jiya”:”Tim”, “IQ”:”College”])
print(df2.iloc[0])
print(df2.iloc[0, 5])
print(df2f2.ilocloc[0:2, 5:8])

# Solution

import pandas as pd, numpy as np

my_di = {“name” : [“Jiya”, “Tim”, “Rohan”],
“age” : np.array([10,15,20]),
“weight” : (75,123,239),
“height” :[4.5, 5, 6.1],
“siblings” : 1,
“gender” : “M”}

df2 = pd.DataFrame(my_di, index=my_di[“name”])
df2[“IQ”] = [130, 105, 115]
df2[“College”] = pd.Series([“IIT”], index=[“Rohan”])

print(df2.loc[“Jiya”]) # prints all the columns for the index ‘Jiya’
print(df2.loc [“Jiya”,”IQ”]) # prints ‘IQ’ column for index ‘Jiya’
print(df2.loc[“Jiya”:”Tim”, “IQ”:”College”]) # prints columns from ‘IQ’ to ‘College’ for indexes from ‘Jiya’ to ‘Tim’
print(df2.iloc[0]) # prints all the columns for the 1st index (O here) i.e. ‘Jiya’
print(df2.iloc[0, 5]) # prints 6th column of the first index
print(df2.iloc[0:2, 5:8]) # prints columns from 6th to 8th (index 5 in 6th index) for first two rows

# Output
name Jiya
age 10
weight 75
height 4.5
siblings 1
gender M
IQ 130
College NaN
Name: Jiya, dtype: object
130
IQ College
Jiya 130 NaN
Tim 105 NaN
name Jiya
age 10
weight 75
height 4.5
siblings 1
gender M
IQ 130
College NaN
Name: Jiya, dtype: object
M
gender IQ College
Jiya M 130 NaN
Tim M 105 NaN