Completed
DataFrame — Row/Column Operations
Questions From This Challenge
Q1mcq1 mark
Which statement correctly adds a new column 'Grade' to an existing DataFrame df by direct assignment?
df['Grade'] = ['A', 'B', 'C'] ✓
df.add('Grade', ['A', 'B', 'C'])
df.append_column('Grade', ['A', 'B', 'C'])
df.new('Grade') = ['A', 'B', 'C']
Q2mcq1 mark
Which statement deletes the column 'Age' from DataFrame df permanently, modifying df in place?
del df['Age'] ✓
df.remove('Age')
df.erase('Age')
df['Age'].delete()
Q3mcq1 mark
Which expression selects the 'Name' column of DataFrame df as a Series?
df['Name'] ✓
df.select('Name')
df{'Name'}
df->Name
Q4mcq1 mark
Which method call correctly renames the column 'Pop' to 'Population' in DataFrame df and returns a new DataFrame?
df.rename(columns={'Pop': 'Population'}) ✓
df.rename('Pop', 'Population')
df.columns.rename('Pop', 'Population')
df.relabel(columns={'Pop': 'Population'})
Q5mcq1 mark
Which method removes a column from a DataFrame and returns a new DataFrame, leaving the original DataFrame unchanged by default?
df.drop('Price', axis=1) ✓
df.delete('Price', axis=1)
df.pop_column('Price')
df.discard('Price')
Q6mcq1 mark
To make df.drop('Marks', axis=1) modify the original DataFrame directly instead of returning a new one, which argument must be added?
inplace=True ✓
modify=True
direct=True
original=True
Q7mcq1 mark
Which expression selects both the 'Name' and 'Marks' columns of df together as a DataFrame (not a Series)?
df[['Name', 'Marks']] ✓
df['Name', 'Marks']
df.loc['Name', 'Marks']
df('Name', 'Marks')
Q8output1 mark
What is the output of the following code that adds a new column to a DataFrame?
import pandas as pd
df = pd.DataFrame({'Name': ['Amit', 'Bina', 'Chetan'], 'Marks': [85, 90, 78]})
df['Grade'] = ['A', 'A', 'B']
print(df)
Correct output:
Name Marks Grade
0 Amit 85 A
1 Bina 90 A
2 Chetan 78 BQ9output1 mark
What is the output of the following code that selects a single column?
import pandas as pd
df = pd.DataFrame({'Name': ['Amit', 'Bina', 'Chetan'], 'Marks': [85, 90, 78]})
print(df['Name'])
Correct output:
0 Amit
1 Bina
2 Chetan
Name: Name, dtype: objectQ10output1 mark
What is the output of the following code that deletes a column using del?
import pandas as pd
df = pd.DataFrame({'Name': ['Amit', 'Bina', 'Chetan'], 'Age': [16, 17, 16], 'Marks': [85, 90, 78]})
del df['Age']
print(df)
Correct output:
Name Marks
0 Amit 85
1 Bina 90
2 Chetan 78Q11output1 mark
What is the output of the following code that renames a column?
import pandas as pd
df = pd.DataFrame({'City': ['Delhi', 'Mumbai', 'Pune'], 'Pop': [30, 20, 10]})
df = df.rename(columns={'Pop': 'Population'})
print(df)
Correct output:
City Population
0 Delhi 30
1 Mumbai 20
2 Pune 10Q12output1 mark
What is the output of the following code, which adds a column computed from two existing columns?
import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df['C'] = df['A'] + df['B']
print(df)
Correct output:
A B C
0 1 4 5
1 2 5 7
2 3 6 9Q13output1 mark
What is the output of the following code that drops a column in place?
import pandas as pd
df = pd.DataFrame({'Item': ['Pen', 'Book', 'Bag'], 'Price': [10, 50, 200]})
df.drop('Price', axis=1, inplace=True)
print(df)
Correct output:
Item
0 Pen
1 Book
2 BagQ14output1 mark
What is the output of the following code, which drops a column into a new DataFrame using the columns parameter?
import pandas as pd
df = pd.DataFrame({'P': [1, 2], 'Q': [3, 4], 'R': [5, 6]})
df2 = df.drop(columns=['Q'])
print(df2)
Correct output:
P R
0 1 5
1 2 6Q15output1 mark
What is the output of the following code that adds a new column using a single scalar value?
import pandas as pd
df = pd.DataFrame({'x': [1, 2, 3]})
df['y'] = 0
print(df)
Correct output:
x y
0 1 0
1 2 0
2 3 0