Comprehension

Temperature, Rainfall, and Humidity Data for 5 States
StateAvg Temp (°C)Rainfall (in cm)Humidity
Assam2015010.6
Delhi30707.5
Kerala2012010.9
Rajasthan35505.6
Telangana28908.7

Using the above data frame answer the questions:

Question: 1

Identify the correct code to remove the column Humidity from the given dataframe, stated:

Show Hint

To remove a column in a pandas dataframe, use `drop()` with `axis = 1` for columns, and `axis = 0` for rows. The `pop()` method removes a column but also returns it.
Updated On: Apr 24, 2025
  • statedf = statedf.pop('Humidity')
    print(statedf)
  • statedf = statedf.pop(['Humidity'], axis = 1)
  • statedf = statedf.drop(['Humidity'], axis = 0)
  • statedf = statedf.drop(['Humidity'], axis = 1)
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is D

Solution and Explanation

The correct way to remove a column in a pandas dataframe is by using the `drop()` function with `axis = 1` to specify that you are removing a column (as opposed to a row). The `pop()` method is used for removing a column but does not support the `axis` parameter. Therefore, the correct syntax is: \[ \text{statedf = statedf.drop(['Humidity'], axis = 1)} \] Therefore, the correct answer is option (4).
Was this answer helpful?
0
0
Question: 2

Identify the correct statement from the following to display all the data of "Rajasthan" state.

Show Hint

The `.loc[]` method in pandas is used to access data by row labels. Ensure to provide the correct label (in this case, 'Rajasthan') for accessing the row.
Updated On: Apr 24, 2025
  • statedf.loc[3]
  • statedf.loc['Rajasthan']
  • statedf.loc(3)
  • statedf.loc('Rajasthan')
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

To display all the data of the state "Rajasthan" from the dataframe, the correct syntax is `statedf.loc['Rajasthan']`. The `loc[]` function in pandas is used to access a group of rows and columns by labels or a boolean array. Here, 'Rajasthan' is the label of the row, and using `.loc` accesses the entire row of that state.
Therefore, the correct answer is option (2).
Was this answer helpful?
0
0
Question: 3

Name the attribute used to display a tuple showing the dimensions of statedf dataframe i.e., (5, 4), 5 rows and 4 columns respectively.

Show Hint

To check the dimensions of a pandas dataframe, use the `.shape` attribute. It returns a tuple representing the number of rows and columns.
Updated On: Apr 24, 2025
  • statedf.size
  • statedf.shape
  • statedf.ndim
  • statedf.index
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is B

Solution and Explanation

The `shape` attribute in pandas is used to display the dimensions of a DataFrame, which returns a tuple showing the number of rows and columns. For the given dataframe, `statedf.shape` will return a tuple (5, 4), indicating that the dataframe has 5 rows and 4 columns.
Therefore, the correct answer is "statedf.shape" (Option 2).
Was this answer helpful?
0
0
Question: 4

.......... command is used to display first five records and ............... command is used to display bottom five records of the dataframe.

Show Hint

Use `head()` to view the top rows and `tail()` to view the bottom rows of a pandas dataframe. Both can take an argument to display the specified number of rows.
Updated On: Apr 24, 2025
  • statedf.head(5), statedf.tail(-5)
  • statedf.head(), statedf.tail(-5)
  • statedf.head(5), statedf.tail(5)
  • statedf.head(-5), statedf.tail(5)
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is C

Solution and Explanation

In pandas, the `head()` function is used to display the first few records of the dataframe, and by default, it shows the first 5 rows if no argument is passed. The `tail()` function displays the bottom rows of the dataframe. To display the bottom 5 records, we use `tail(5)`. Therefore, the correct statement is: \[ \text{statedf.head(5)}, \text{statedf.tail(5)} \] Thus, the correct answer is option (3).
Was this answer helpful?
0
0
Question: 5

Give the output of the given python code: 8.7 statedf.loc[:, ’Rainfall (in cm)’] > 90

Show Hint

The `.loc[]` method in pandas is powerful for selecting rows and columns based on specific conditions. Here we used it to filter rows where a condition on a specific column holds true.
Updated On: Apr 24, 2025
  • StateValue
    AssamTrue
    DelhiFalse
    KeralaTrue
    RajasthanFalse
    TelanganaFalse
  • StateValue
    AssamTrue
    DelhiFalse
    KeralaTrue
    RajasthanFalse
    TelanganaFalse
  • 1150
    3120
  • Assam150
    Kerala120
Hide Solution
collegedunia
Verified By Collegedunia

The Correct Option is A

Solution and Explanation

The given Python code is used to filter the "Rainfall (in cm)" column and check which values are greater than 90. The `loc` function is used to access all rows (indicated by `:`) of the 'Rainfall (in cm)' column, and then the condition `> 90` is applied. The output will return `True` for the rows where the rainfall is greater than 90 and `False` otherwise. The result is:
- Assam: 150 cm>90, so True
- Delhi: 70 cm ≤ 90, so False
- Kerala: 120 cm>90, so True
- Rajasthan: 50 cm ≤ 90, so False
- Telangana: 90 cm ≤ 90, so False
Therefore, the correct output is option (1).
Was this answer helpful?
0
0

Questions Asked in CUET exam

View More Questions