Complete the code to add a new column 'C' with all values set to 10.
import pandas as pd df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) df['C'] = [1] print(df)
To add a new column with the same value for each row, you can assign a list with the values. Here, [10, 10] matches the number of rows.
Complete the code to remove the column 'B' from the DataFrame.
import pandas as pd df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) df = df.[1]('B', axis=1) print(df)
remove or delete which are not pandas DataFrame methods.axis=1 to specify column removal.The drop method removes columns or rows. To remove a column, specify the column name and axis=1.
Fix the error in the code to add a new column 'D' as the sum of columns 'A' and 'B'.
import pandas as pd df = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) df['D'] = df['A'] [1] df['B'] print(df)
To add two columns element-wise, use the plus operator +.
Fill both blanks to create a new column 'E' with values from column 'A' multiplied by 2, but only for rows where 'B' is greater than 3.
import pandas as pd df = pd.DataFrame({'A': [1, 2, 3], 'B': [3, 4, 5]}) df['E'] = df['A'] [1] 2 filtered = df[df['B'] [2] 3] print(filtered)
Multiply column 'A' by 2 using *. Filter rows where 'B' is greater than 3 using >.
Fill all three blanks to create a dictionary with keys as uppercase column names and values as the mean of each column, but only for columns where the mean is greater than 2.
import pandas as pd df = pd.DataFrame({'a': [1, 3, 5], 'b': [2, 4, 6], 'c': [0, 1, 1]}) result = { [1]: df[[2]].mean() for [3] in df.columns if df[[3]].mean() > 2 } print(result)
The dictionary comprehension uses col.upper() as keys, col to select columns, and iterates over df.columns.