Informatics Practices
Assume that required libraries (panda and numpy) are imported and dataframe df2 has been created as per questions 17 and 18 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(df2.iloc[0:2, 5:8])
Python Pandas
1 Like
Answer
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
Working
print(df2.loc["Jiya"])— This line prints all columns of the row with the index "Jiya".print(df2.loc["Jiya", "IQ"])— This line prints the value of the "IQ" column for the row with the index "Jiya".print(df2.loc["Jiya":"Tim", "IQ":"College"])— This line prints a subset of rows and columns using labels, from "Jiya" to "Tim" for rows and from "IQ" to "College" for columns.print(df2.iloc[0])— This line prints all columns of the first row using integer-based indexing (position 0).print(df2.iloc[0, 5])— This line prints the value of the 6th column for the first row using integer-based indexing.print(df2.iloc[0:2, 5:8])— This line prints a subset of rows and columns using integer-based indexing, selecting rows from position 0 to 1 and columns from position 5 to 7.
Answered By
1 Like
Related Questions
Assume that required libraries (panda and numpy) are imported and dataframe df2 has been created as per questions 17 and 18 above. Predict the output of following code fragment :
df2["IQ"] = [130, 105, 115] df2["Married"] = False print(df2)Assume that required libraries (panda and numpy) are imported and dataframe df2 has been created as per questions 17 and 18 above. Predict the output produced by following code fragment :
What is the output of the following code ?
d = {'col1': [1, 4, 3 ], 'col2': [6, 7, 8], 'col3': [9, 0, 1]} df = pd.DataFrame(d) print("Original DataFrame") print(df) print("New DataFrame :") dfn = df.drop(df.index[[1, 2]]) print(dfn)What is the output of the following code ?
data = {'age': [20, 23, 22], 'name': ['Ruhi', 'Ali', 'Sam']} df1 = pd.DataFrame(data, index=[1, 2, 3]) print("Before") print(df1) df1['Edu'] = ['BA', 'BE' , 'MBA'] print('After') print(dfl)