What is the output of the following python code?
import pandas as pd
seriesMnths = pd.Series([2,3,4],index=["Feb ","Mar","Apr"])
print(seriesMnths["Mar"] )
What is the output of the following python code?
import pandas as pd
seriesMnths = pd.Series([2,3,4],index=["Feb ","Mar","Apr"])
print(seriesMnths["Mar"] )
Solution
The Series pairs each label in the index with a value: "Feb " with 2, "Mar" with 3 and "Apr" with 4. Writing seriesMnths["Mar"] looks up the value stored at the label "Mar", so it prints 3.
Option 1: "Mar" is the label, not the value. Indexing with a label returns the value, not the label itself.
Option 2: 1 would be the position of "Mar" if you counted from 1. Python positions start at 0, so the position of "Mar" is 1 (not 2), but the output is the value at that position, not the position itself.
Option 4: 2 is the value for "Feb ", the first label, not for "Mar".
Related questions:
2025: 3 June Shift 2
2026: 29 May Shift 2