Complete the code to create a DataFrame with duplicate column names.
import pandas as pd data = {'A': [1, 2], [1]: [3, 4]} df = pd.DataFrame(data) print(df)
The dictionary keys are column names. Using 'A' twice creates duplicate columns.
Complete the code to rename duplicate columns by adding suffixes.
df.columns = [col + '_1' if df.columns.duplicated()[i] else col for i, col in enumerate([1])] print(df.columns)
df.index instead of df.columns.We check duplicates in column names using df.columns.duplicated() and rename accordingly.
Fix the error in the code to select the first 'A' column from a DataFrame with duplicate columns.
first_A = df[[1]] print(first_A)
df.loc[:, 'A'] which may cause ambiguity with duplicates.Using df['A'] selects the first column named 'A' even if duplicates exist.
Fill both blanks to create a dictionary comprehension that keeps only columns with unique names.
unique_cols = {col: df[col] for col in df.columns if df.columns.[1](keep=False)[df.columns.[2](col)] == False}unique() which returns unique values, not a boolean mask.tolist() which converts to list, not for checking duplicates.df.columns.duplicated(keep=False)[df.columns.get_loc(col)] returns True if the column name appears more than once. We keep columns where this is False.
Fill all three blanks to create a DataFrame with unique column names by appending suffixes to duplicates.
cols = []
counter = {}
for col in df.columns:
if col in counter:
counter[col] += 1
cols.append(col + [1] + str(counter[col]))
else:
counter[col] = 0
cols.append(col + [2])
df.columns = cols
print(df.columns)We add an underscore and a number suffix to duplicate columns. The first occurrence has no suffix.