If the data needed for ticks is taken from an index of a DataFrame 'df', then which of the following command is needed?
If the data needed for ticks is taken from an index of a DataFrame 'df', then which of the following command is needed?
Solution
df.index returns the Index object of the DataFrame, and its tolist() method converts it into a Python list. So ticks = df.index.tolist() gives the row labels as a list, which can be passed to a plot's tick settings such as plt.xticks(ticks).
Option 1: list() is a built-in function, not a method of an Index object, so df.index.list() raises an AttributeError.
Option 3: df[index] looks for a column named by the variable index, which is not defined, and does not access the DataFrame's index.
Option 4: df.list is not a DataFrame attribute, and square brackets are not how methods are called.
You can also write list(df.index), which gives the same result.
Related questions:
2025: 22 May Shift 1
2025: 14 May Shift 1
2025: 22 May Shift 1
2026: 23 May Shift 2