Texas California 1 44 45 2 55 34 3 23 67 4 67 33
For the DataFrame (df) given above, which of the following are correct ways to access column 'Texas'?
A. df['Texas']
B. df('Texas')
C. df.Texas
D. df.['Texas']
| Texas | California | |
|---|---|---|
| 1 | 44 | 45 |
| 2 | 55 | 34 |
| 3 | 23 | 67 |
| 4 | 67 | 33 |
For the DataFrame (df) given above, which of the following are correct ways to access column 'Texas'?
A. df['Texas']
B. df('Texas')
C. df.Texas
D. df.['Texas']
Solution
df['Texas'] uses square-bracket indexing with the column label, which is the standard way to select a column and returns it as a Series.
df.Texas uses attribute-style access. It works because the column name is a valid Python identifier and does not clash with an existing DataFrame attribute or method.
So A and C are correct.
B: df('Texas') uses round brackets, which would try to call the DataFrame as a function. A DataFrame is not callable, so this raises a TypeError.
D: df.['Texas'] is a syntax error, because a dot cannot be placed directly before square brackets.
Related questions:
2026: 31 May Shift 1
2026: 26 May Shift 1
2026: 21 May Shift 1