Search Header Logo

Pandas_Ops

Authored by Navneet Sadh

Computers

12th Grade

Used 3+ times

Pandas_Ops
AI

AI Actions

Add similar questions

Adjust reading levels

Convert to real-world scenario

Translate activity

More...

    Content View

    Student View

9 questions

Show all answers

1.

MULTIPLE CHOICE QUESTION

1 min • 1 pt

How do you select all rows and only specific columns in a DataFrame using the loc() method?

df.loc[:, columns]

df.loc[rows, :]

df.loc[rows, columns]

df.loc[:, rows]

Answer explanation

To select all rows and only specific columns in a DataFrame using the loc method, we can pass the : and columns as arguments to the loc method. Using df.loc[:, columns] will select all rows and only the specified columns.

2.

MULTIPLE CHOICE QUESTION

1 min • 1 pt

Which slicing operation is used to select rows from index 2 to index 5 (inclusive) in a DataFrame?

df.loc[2:6]

df.iloc[2:5]

df.iloc[2:6]

df.loc[2:5]

Answer explanation

Slicing notation df.iloc[2:6] is used to select rows from index 2 to index 5 (inclusive) in a DataFrame. The ending index is exclusive, so using [2:6] will select rows with indices 2, 3, 4 and 5.

3.

MULTIPLE CHOICE QUESTION

1 min • 1 pt

How do you add a new row to a DataFrame using the loc() method?

df.loc[row_label] = row_data

df.loc[:, row_label] = row_data

df.loc[row_data, row_label]

df.loc[row_data] = row_label

Answer explanation

To add a new row to a DataFrame using the loc method, we can assign the row data to a new row label. Using df.loc[row_label] = row_data will add a new row with the specified label and corresponding data.

4.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt


  1. Given a DataFrame df, write a code snippet to delete the column named Salary.

df.drop(Salary, axis=1)

df.drop('Salary',inplace=True)

df.remove(Salary)

df=df.drop('Salary',axis=1,inplace=False)

Answer explanation

To delete a column named "Salary" from a DataFrame, we can use the drop method with axis=1 to indicate that we are dropping a column. Using df=df.drop("Salary", axis=1,inplace=False) will delete the "Salary" column.

5.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

How do you delete a third row named row_3 from a DataFrame using the drop() method?

df.drop(row_3,inplace=True)

df.drop(df.index[2],inplace='True')

df.drop('row_3',axis=1,inplace='True')

df.drop('row_3')

Answer explanation

To delete a specific row named "row_3" from a DataFrame, we can use the drop method with:

df.index[2] to get the label of third row.

axis=0

and

inplace=True

6.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

Given a DataFrame df with columns A and B, write a code snippet to rename the row index from 'old_index' to 'new_index'.

df=df.rename(index={'old_index': 'new_index'})

df=df.rename(index=['old_index', 'new_index'])

df=df.rename(index='old_index', new_index='new_index')

df.rename({'old_index': 'new_index'},axis=0)

Answer explanation

To rename the row index from "old_index" to "new_index" in a DataFrame, we can use the rename method and pass a dictionary to the index parameter. Using df.rename(index={'old_index': 'new_index'}) will rename the row index accordingly.
As iplace is false by default save the copy in the df itself.

7.

MULTIPLE CHOICE QUESTION

2 mins • 1 pt

How do you delete columns with names ['col1', 'col2', 'col3'] from a DataFrame df using the drop() method?

df.drop(['col1', 'col2', 'col3'])

df=df.drop(['col1', 'col2', 'col3'], axis=0)

df.drop(['col1', 'col2', 'col3'], axis=1)

df.drop(['col1', 'col2', 'col3'],axis=1, inplace=True)

Answer explanation

To delete columns with names "col1", "col2", and "col3" from a DataFrame, we can use the drop method with axis=1 to indicate that we are dropping columns. Using

df.drop(['col1', 'col2', 'col3'],axis=1, inplace=True) will delete the specified columns.

Access all questions and much more by creating a free account

Create resources

Host any resource

Get auto-graded reports

Google

Continue with Google

Email

Continue with Email

Classlink

Continue with Classlink

Clever

Continue with Clever

or continue with

Microsoft

Microsoft

Apple

Apple

Others

Others

Already have an account?