Skip to main contentSkip to solution

If the data needed for ticks is taken from an index of a DataFrame 'df', then which of the following command is needed?

Solution

✅ Correct Option: 2

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.

Keyboard Shortcuts

  • Left arrow: Previous question
  • Right arrow: Next question
  • S key: Jump to solution
  • Q key: Jump to question