0
0
Pandasdata~10 mins

Specifying column names and index in Pandas - 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 columns named 'A' and 'B'.

Pandas
import pandas as pd
data = [[1, 2], [3, 4]]
df = pd.DataFrame(data, columns=[1])
print(df)
Drag options to blanks, or click blank then click option'
A['a', 'b']
B['X', 'Y']
C['1', '2']
D['A', 'B']
Attempts:
3 left
💡 Hint
Common Mistakes
Using column names that don't match the data length.
Not passing a list for the columns parameter.
2fill in blank
medium

Complete the code to set the index of the DataFrame to the 'Name' column.

Pandas
import pandas as pd
data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
df = df.set_index([1])
print(df)
Drag options to blanks, or click blank then click option'
A'Index'
B'Name'
C'Age'
D'ID'
Attempts:
3 left
💡 Hint
Common Mistakes
Using a column name that does not exist.
Passing the column name without quotes.
3fill in blank
hard

Fix the error in the code to correctly specify column names when creating the DataFrame.

Pandas
import pandas as pd
data = [[5, 6], [7, 8]]
df = pd.DataFrame(data, columns=[1])
print(df)
Drag options to blanks, or click blank then click option'
A['A', 'B']
B'A, B'
C('A', 'B')
D{'A', 'B'}
Attempts:
3 left
💡 Hint
Common Mistakes
Using a single string with commas instead of a list.
Using a tuple or set instead of a list.
4fill in blank
hard

Fill both blanks to create a DataFrame with columns 'X' and 'Y' and set 'X' as the index.

Pandas
import pandas as pd
data = [[9, 10], [11, 12]]
df = pd.DataFrame(data, columns=[1])
df = df.set_index([2])
print(df)
Drag options to blanks, or click blank then click option'
A['X', 'Y']
B['A', 'B']
C'X'
D'Y'
Attempts:
3 left
💡 Hint
Common Mistakes
Setting the index to a column not in the DataFrame.
Using incorrect list or string syntax.
5fill in blank
hard

Fill all three blanks to create a DataFrame with columns 'Name' and 'Score', set 'Name' as index, and print the DataFrame.

Pandas
import pandas as pd
data = [['John', 88], ['Jane', 92]]
df = pd.DataFrame(data, columns=[1])
df = df.set_index([2])
print([3])
Drag options to blanks, or click blank then click option'
A['Name', 'Score']
B'Name'
Cdf
D'Score'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect syntax for columns or index.
Printing the wrong variable.