How do you add a new column to a pandas DataFrame with a fixed value?
You can add a new column by assigning a value to a new column name like this: df['new_column'] = 10 This sets the value 10 for all rows in the new column.
Click to reveal answer
beginner
What is the syntax to create a new column based on existing columns in pandas?
You can create a new column by performing operations on existing columns: df['new_col'] = df['col1'] + df['col2'] This adds values from 'col1' and 'col2' row-wise.
Click to reveal answer
intermediate
How can you create a new column using a function applied to each row in pandas?
Use the apply() method with axis=1 to apply a function row-wise: df['new_col'] = df.apply(lambda row: row['col1'] * 2, axis=1)
Click to reveal answer
beginner
What happens if you assign a list of values to a new column in pandas?
If the list length matches the number of rows, pandas assigns each list item to the corresponding row in the new column. Example: df['new_col'] = [1, 2, 3] for a DataFrame with 3 rows.
Click to reveal answer
intermediate
How do you create a new column conditionally in pandas?
Use np.where() or boolean indexing: df['new_col'] = np.where(df['col'] > 5, 'High', 'Low') This sets 'High' if value > 5, else 'Low'.
Click to reveal answer
Which of the following adds a new column 'age_plus_10' that adds 10 to the 'age' column?
Adf['age_plus_10'] = df['age'] - 10
Bdf['age_plus_10'] = 10
Cdf['age_plus_10'] = df['age'] * 10
Ddf['age_plus_10'] = df['age'] + 10
✗ Incorrect
Adding 10 to the 'age' column creates a new column with each age increased by 10.
What does this code do? df['new_col'] = df.apply(lambda row: row['a'] * 2, axis=1)
ACreates a new column doubling values in column 'a' for each row
BCreates a new column with the sum of all columns
CDeletes column 'a'
DFilters rows where 'a' is doubled
✗ Incorrect
The apply function with axis=1 applies the lambda to each row, doubling the 'a' value.
If you assign a list shorter than the DataFrame rows to a new column, what happens?
APandas raises an error
BPandas fills missing rows with NaN
CPandas repeats the list values
DPandas ignores the assignment
✗ Incorrect
The list length must match the number of rows; otherwise, pandas raises a ValueError.
How to create a new column 'status' that is 'Adult' if 'age' >= 18, else 'Minor'?
Hint: Use df['new_col'] = expression to add columns [OK]
Common Mistakes:
Using double equals (==) instead of single equals (=) for assignment
Trying to use a method like add_column which doesn't exist
Using dot notation for new columns with spaces or special characters
2. Which of the following is the correct syntax to create a new column full_name by combining first_name and last_name columns with a space in between in DataFrame df?
easy
A. df['full_name'] = df['first_name'] + ' ' + df['last_name']
B. df['full_name'] = df['first_name'].append(' ').append(df['last_name'])
C. df['full_name'] = df['first_name'].join(' ', df['last_name'])
D. df['full_name'] = df['first_name'] & ' ' & df['last_name']
Solution
Step 1: Combine string columns with + operator
In pandas, string columns can be combined using the + operator. To add a space, include ' ' between them.
Step 2: Assign the combined string to a new column
Assign the result to df['full_name'] to create the new column.
Multiply each row's price by quantity: 100*2=200, 150*3=450, 200*1=200.
Step 2: Assign result to new column and output it
The new column 'total' will have values [200, 450, 200]. Printing df['total'] shows this list.
Final Answer:
[200, 450, 200] -> Option A
Quick Check:
Element-wise multiply = [200, 450, 200] [OK]
Hint: Multiply columns element-wise for new column [OK]
Common Mistakes:
Adding instead of multiplying columns
Confusing row-wise with column-wise operations
Expecting a scalar instead of a Series output
4. What is wrong with this code that tries to create a new column discounted_price by subtracting 10 from price in DataFrame df?
df.discounted_price = df['price'] - 10
medium
A. It will fail if the column name has spaces or special characters.
B. It will cause a syntax error because of dot notation.
C. It will create a new variable, not a DataFrame column.
D. It will create the column correctly.
Solution
Step 1: Understand dot notation for DataFrame columns
Dot notation df.col = value works to create new columns only if 'col' is a valid Python identifier (no spaces, no special characters).
Step 2: Check the column name 'discounted_price'
'discounted_price' consists of letters and underscores, which is valid. Therefore, the code creates the column correctly.
Final Answer:
It will create the column correctly. -> Option D
Quick Check:
Dot notation works for valid identifiers [OK]
Hint: Use df['col'] if column name has spaces or special chars [OK]
Common Mistakes:
Thinking dot notation fails for valid column names
Confusing DataFrame column assignment with local variable creation
Believing underscores invalidate column names for dot notation
5. You have a DataFrame df with columns score and max_score. You want to create a new column percentage that shows the score as a percentage of max_score, but if max_score is zero, set percentage to None. Which code correctly does this?
hard
A. df['percentage'] = df['score'] / df['max_score'].replace(0, None) * 100