0
0
Data Analysis Pythondata~10 mins

Handling duplicate column names in Data Analysis Python - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a DataFrame with duplicate column names.

Data Analysis Python
import pandas as pd

data = {'A': [1, 2], [1]: [3, 4]}
df = pd.DataFrame(data)
print(df)
Drag options to blanks, or click blank then click option'
A'A'
B'B'
C'C'
D'D'
Attempts:
3 left
💡 Hint
Common Mistakes
Using different keys will not create duplicate columns.
Forgetting to use quotes around column names.
2fill in blank
medium

Complete the code to rename duplicate columns by adding suffixes.

Data Analysis Python
df.columns = [col + '_1' if df.columns.duplicated()[i] else col for i, col in enumerate([1])]
print(df.columns)
Drag options to blanks, or click blank then click option'
Adf.columns
Bdf.head()
Cdf.values
Ddf.index
Attempts:
3 left
💡 Hint
Common Mistakes
Using df.index instead of df.columns.
Trying to rename without checking duplicates.
3fill in blank
hard

Fix the error in the code to select the first 'A' column from a DataFrame with duplicate columns.

Data Analysis Python
first_A = df[[1]]
print(first_A)
Drag options to blanks, or click blank then click option'
A'A'
B['A']
Cdf['A']
Ddf.columns[0]
Attempts:
3 left
💡 Hint
Common Mistakes
Using df.loc[:, 'A'] which may cause ambiguity with duplicates.
Using a list instead of a string for column selection.
4fill in blank
hard

Fill both blanks to create a dictionary comprehension that keeps only columns with unique names.

Data Analysis Python
unique_cols = {col: df[col] for col in df.columns if df.columns.[1](keep=False)[df.columns.[2](col)] == False}
Drag options to blanks, or click blank then click option'
Aduplicated
Bunique
Ctolist
Dget_loc
Attempts:
3 left
💡 Hint
Common Mistakes
Using unique() which returns unique values, not a boolean mask.
Using tolist() which converts to list, not for checking duplicates.
5fill in blank
hard

Fill all three blanks to create a DataFrame with unique column names by appending suffixes to duplicates.

Data Analysis Python
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)
Drag options to blanks, or click blank then click option'
A'_'
B''
C'-'
D'#'
Attempts:
3 left
💡 Hint
Common Mistakes
Adding suffix to the first occurrence.
Using no separator between name and number.