Question:medium

For the DataFrame (df) given below, which of the following are correct ways to access column 'Texas'?
A. df['Texas']
B. df('Texas')
C. df.Texas
D. df.['Texas']

Show Hint

While dot notation (df.Texas) is shorter to type, bracket notation (df['Texas']) is safer because it works even if column names contain spaces or special characters.
Updated On: Jun 11, 2026
  • B and D only
  • A and C only
  • B, C and D only
  • A, B and C only
Show Solution

The Correct Option is B

Solution and Explanation


Step 1: Understanding the Question:

The question asks us to identify the correct syntax in Pandas to retrieve or access a single column named 'Texas' from a given DataFrame df.

Step 2: Key Formula or Approach:

In Pandas, there are two primary syntaxes to access a single column of a DataFrame:
1. Dictionary-like notation: df['column_name']
2. Attribute notation: df.column_name (this works only if the column name is a valid Python identifier and doesn't conflict with existing DataFrame method names).

Step 3: Detailed Explanation:

Let's analyze the statements:
- A. df['Texas']: This is correct. It uses dictionary-like square bracket notation to access the column label.
- B. df('Texas'): This is incorrect. DataFrames are not callable functions; using parentheses will raise a TypeError.
- C. df.Texas: This is correct. It uses attribute notation, which is valid since 'Texas' contains no spaces and is a valid identifier.
- D. df.['Texas']: This is incorrect. Combining attribute dot notation with square brackets is invalid Python syntax and will throw a SyntaxError.
Therefore, options A and C are the only correct ways.
This matches option (B).

Step 4: Final Answer:

The correct option is (B), meaning A and C only.
Was this answer helpful?
0


Questions Asked in CUET (UG) exam