0
0
Pandasdata~20 mins

Creating DataFrame from list of lists in Pandas - Practice Exercises

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
DataFrame Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this DataFrame creation code?
Given the code below, what will be the content of the DataFrame df?
Pandas
import pandas as pd

data = [[1, 2], [3, 4], [5, 6]]
df = pd.DataFrame(data, columns=['A', 'B'])
print(df)
A
   A  B
0  1  2
1  3  4
2  5  6
B
   A  B
0  1  3
1  2  4
2  5  6
C
   0  1
0  1  2
1  3  4
2  5  6
DError: Missing columns argument
Attempts:
2 left
💡 Hint
Check how columns are assigned when creating a DataFrame from a list of lists.
data_output
intermediate
1:30remaining
How many rows and columns does this DataFrame have?
Consider this code creating a DataFrame from a list of lists. How many rows and columns does df have?
Pandas
import pandas as pd

data = [[10, 20, 30], [40, 50, 60]]
df = pd.DataFrame(data)
print(df.shape)
A(3, 3)
B(3, 2)
C(2, 3)
D(2, 2)
Attempts:
2 left
💡 Hint
Each inner list is a row; count rows and columns accordingly.
🔧 Debug
advanced
2:00remaining
Why does this DataFrame creation code raise an error?
Look at this code. Why does it raise an error?
Pandas
import pandas as pd

data = [[1, 2], [3, 4]]
df = pd.DataFrame(data, columns=['A'])
ASyntaxError due to missing brackets
BTypeError because columns must be integers
CNo error, DataFrame created with one column
DValueError because number of columns does not match data width
Attempts:
2 left
💡 Hint
Check if the number of columns matches the number of elements in each inner list.
🚀 Application
advanced
2:30remaining
Create a DataFrame with custom row labels from list of lists
You have this list of lists: data = [[7, 8], [9, 10]]. You want to create a DataFrame with columns named X and Y and row labels row1 and row2. Which code does this correctly?
Apd.DataFrame(data, columns=['X', 'Y'], index=['row1', 'row2'])
Bpd.DataFrame(data, index=['X', 'Y'], columns=['row1', 'row2'])
Cpd.DataFrame(data, columns=['row1', 'row2'], index=['X', 'Y'])
Dpd.DataFrame(data, labels=['X', 'Y'], rows=['row1', 'row2'])
Attempts:
2 left
💡 Hint
Remember the parameters for columns and index in DataFrame constructor.
🧠 Conceptual
expert
3:00remaining
What happens if inner lists have different lengths when creating a DataFrame?
Consider this code:
import pandas as pd
data = [[1, 2], [3, 4, 5]]
df = pd.DataFrame(data)
What will be the content of df?
ADataFrame with columns named 0 and 1 only
BDataFrame with NaN in missing places, shape (2, 3)
CDataFrame with only first two columns, extra data ignored
DValueError due to inconsistent row lengths
Attempts:
2 left
💡 Hint
Pandas fills missing values with NaN when rows have different lengths.