Skip to main contentSkip to solution

Help Mohan for displaying a column of a DataFrame 'DF' as he is confused among the following options.

(A) DF.loc['COLUMN NAME']

(B) DF[COLUMN NAME]

(C) DF.COLUMN NAME

(D) DF['COLUMN NAME']

Solution

✅ Correct Option: 4

A column of a DataFrame can be displayed by putting its name in quotes inside square brackets, DF[′COLUMNNAME′]DF['COLUMN NAME'], as in (D). It can also be displayed with dot notation, DF.COLUMNNAMEDF.COLUMN NAME, as in (C). Dot notation works only when the column name is a valid identifier, with no spaces or special characters.


Option (A): locloc takes a row label first, so DF.loc[′COLUMNNAME′]DF.loc['COLUMN NAME'] looks for a row with that name and not a column.

Option (B): without quotes, Python treats COLUMN NAME as a variable name, which raises a NameError.

Use quotes with square brackets whenever the column name has spaces or clashes with a DataFrame method or attribute.

Keyboard Shortcuts

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