Revision
Worksheet: Pandas loc[] and iloc[]
Create the following DataFrame & write
the output of each statement.
|
Index |
Name |
Marks |
Grade |
|
S1 |
Aman |
85 |
B |
|
S2 |
Riya |
92 |
A |
|
S3 |
Kabir |
78 |
C |
|
S4 |
Neha |
88 |
B |
|
S5 |
Arjun |
95 |
A |
Predict
the Output
Q1.
print(df.loc['S3'])
Output:
_____________________________________________________________________
Q2.
print(df.iloc[1:4, 0:2])
Output:
_____________________________________________________________________
Q3.
print(df.loc['S2':'S4', ['Name', 'Marks']])
Output:
_____________________________________________________________________
Q4.
print(df.iloc[-2, 1])
Output:
_____________________________________________________________________
Q5.
print(df.loc[df['Marks'] > 85, ['Name', 'Marks']])
Output:
_____________________________________________________________________
Q6.
print(df.iloc[[0, 2, 4], [0, 2]])
Output:
_____________________________________________________________________
Q7.
print(df.loc['S1':'S3', 'Marks'])
Output:
_____________________________________________________________________
Q8.
print(df.iloc[::2, 1:])
Output:
_____________________________________________________________________
Q9.
print(df.loc[df['Grade'] == 'A', 'Name'])
Output:
_____________________________________________________________________
Q10.
print(df.iloc[2:5:2, 0:3:2])
Output:
_____________________________________________________________________
NOTE:
loc[] uses labels and includes the ending label in a slice;
iloc[] uses integer positions and excludes the
ending position in a slice.
Comments
Post a Comment