Page 454 - AI Ver 3.0 class 10_Flipbook
P. 454
For example:
• To print labelled and positional index
[1]: import pandas as pd
Marks = pd.Series([96, 92, 91, 93], index=["Shashi",
"Sonali", "Neha", "Rajeev"])
print("Using Labelled index ", Marks["Sonali"])
print("Using Positional index ", Marks.iloc[1])
Using Labelled index 92
Using Positional index 92
Note,
✶ Use .iloc[pos] for positional indexing.
✶ Use .loc[label] or [] for label-based indexing.
• To print 2 elements:
[1]: print(Marks.iloc[[2, 3]])
Neha 91
Rajeev 93
dtype: int64
Note,
✶ Use .iloc[[pos1, pos2]] for positional indexing.
✶ Use .loc[[label1, label2]] for label-based indexing.
• To print 2 elements in another way:
[1]: print(Marks[["Sonali","Rajeev"]])
Sonali 92
Rajeev 93
dtype: int64
• To access elements using slicing:
[1]: print(Marks[1:3]) #excludes the value at index position 3
Sonali 92
Neha 91
dtype: int64
• To access elements using slicing in another way:
[1]: print(Marks["Sonali":"Rajeev"])
Sonali 92
Neha 91
Rajeev 93
dtype: int64
• To display the series in the reverse order
[1]: print(Marks[::-1])
Rajeev 93
Neha 91
Sonali 92
Shashi 96
dtype: int64
452 Touchpad Artificial Intelligence (Ver. 3.0)-X

