Pandas_Ops

Pandas_Ops

12th Grade

9 Qs

quiz-placeholder

Similar activities

Keyboarding

Keyboarding

5th - 12th Grade

10 Qs

Dataframe

Dataframe

10th - 12th Grade

12 Qs

Python Pandas/MatPlotLib

Python Pandas/MatPlotLib

12th Grade

9 Qs

Pandas 10-Minute Quiz

Pandas 10-Minute Quiz

12th Grade

10 Qs

Python Pandas Series

Python Pandas Series

12th Grade

10 Qs

Python Pandas

Python Pandas

11th - 12th Grade

10 Qs

Dataframe 1

Dataframe 1

12th Grade

13 Qs

DataFrames

DataFrames

12th Grade

12 Qs

Pandas_Ops

Pandas_Ops

Assessment

Quiz

Computers

12th Grade

Hard

Created by

Navneet Sadh

Used 3+ times

FREE Resource

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.

8.

MULTIPLE CHOICE QUESTION

5 mins • 3 pts

import pandas as pd

data = { 'Name': ['John', 'Emma'], 'Age': [25, 30], 'City': ['New York', 'London'] }

df = pd.DataFrame(data)

What is the resulting DataFrame after performing the following operations on the original DataFrame?

  1. 1, Addition of a new row with the data ['Mike', 35, 'Paris']

  2. 2. Deletion of the column named "City"

  3. 3. Renaming the row index 1 to "Row2" and column name 'Name' to 'First Name'

Name Age

0 John 25

Row2 Emma 30

2 Mike 35

First Name Age

0 John 25

Row2 NaN 30

2 NaN 35

First Name Age

0 John 25

Row2 Emma 30

2 Mike 35

First Name Age City

0 John 25 New York

Row2 NaN 30 NaN

2 NaN 35 NaN

Answer explanation

The original DataFrame df is modified by adding a new row with the data ['Mike', 35, 'Paris'], deleting the column named "City," and renaming the row index 1 to "Row2" and column name 'Name' to 'First Name'. The resulting DataFrame after these operations is as shown in option c).

9.

OPEN ENDED QUESTION

10 mins • 5 pts

Given the DataFrame:

import pandas as pd

data = { 'Name': ['John', 'Emma'], 'Age': [25, 30], 'City': ['New York', 'London'] }

df = pd.DataFrame(data)

Write down the code to perform the following operations on the original DataFrame:

  1. 1. Addition of a new row with the data ['Mike', 35, 'Paris']

  2. 2. Deletion of the column named "City"

  3. 3. Renaming the row index 1 to "Row2" and column name 'Name' to 'First Name'.

Evaluate responses using AI:

OFF

Answer explanation

  1. 1. Addition of a new row with the data ['Mike', 35, 'Paris']

  2. df.loc[2] = ['Mike', 35, 'Paris']

  3. 2. Deletion of the column named "City".

  4. df = df.drop('City', axis=1)

  5. 3. Renaming the row index 1 to "Row2" and column name 'Name' to 'First Name'.

  6. df = df.rename(index={1: 'Row2'}, columns={'Name': 'First Name'})