Bird
Raised Fist0
Pandasdata~20 mins

Creating DataFrame from list of lists in Pandas - Practice Exercises

Choose your learning style10 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.

Practice

(1/5)
1. What does creating a DataFrame from a list of lists in pandas mainly help you do?
easy
A. Organize data into rows and columns for easy analysis
B. Run SQL queries directly on the list
C. Convert data into images automatically
D. Encrypt data for security

Solution

  1. Step 1: Understand DataFrame purpose

    A DataFrame is a table-like structure with rows and columns to organize data.
  2. Step 2: Connect list of lists to DataFrame

    A list of lists can be converted into this table format for easier data handling.
  3. Final Answer:

    Organize data into rows and columns for easy analysis -> Option A
  4. Quick Check:

    DataFrame = Table format [OK]
Hint: DataFrames = tables; list of lists = rows [OK]
Common Mistakes:
  • Thinking DataFrame encrypts data
  • Confusing DataFrame with image conversion
  • Assuming list of lists runs SQL
2. Which of the following is the correct way to create a DataFrame from a list of lists data = [[1, 2], [3, 4]] with columns named 'A' and 'B'?
easy
A. pd.DataFrame(data, columns=['A', 'B'])
B. pd.DataFrame(data, names=['A', 'B'])
C. pd.DataFrame(data, cols=['A', 'B'])
D. pd.DataFrame(data, header=['A', 'B'])

Solution

  1. Step 1: Recall DataFrame column naming syntax

    Use the parameter columns to name columns when creating a DataFrame.
  2. Step 2: Check each option

    Only pd.DataFrame(data, columns=['A', 'B']) uses columns=['A', 'B'], which is correct syntax.
  3. Final Answer:

    pd.DataFrame(data, columns=['A', 'B']) -> Option A
  4. Quick Check:

    Use columns= for column names [OK]
Hint: Use columns= to name DataFrame columns [OK]
Common Mistakes:
  • Using names= instead of columns=
  • Using cols= or header= which are invalid here
  • Forgetting to pass columns parameter
3. What will be the output of the following code?
import pandas as pd
data = [[10, 20], [30, 40]]
df = pd.DataFrame(data, columns=['X', 'Y'])
print(df)
medium
A. X Y 0 20 10 1 40 30
B. X Y 0 10 20 1 30 40
C. 0 1 X 10 20 Y 30 40
D. Error: columns parameter missing

Solution

  1. Step 1: Create DataFrame with given data and columns

    The list of lists becomes rows; columns=['X', 'Y'] names columns accordingly.
  2. Step 2: Print DataFrame

    Output shows index 0 and 1 with values 10,20 and 30,40 under columns X and Y.
  3. Final Answer:

    X Y 0 10 20 1 30 40 -> Option B
  4. Quick Check:

    DataFrame prints rows with columns X, Y [OK]
Hint: Rows = inner lists; columns = names list [OK]
Common Mistakes:
  • Swapping row and column values
  • Confusing index with columns
  • Expecting error without columns parameter
4. Identify the error in this code snippet:
import pandas as pd
data = [[1, 2], [3, 4]]
df = pd.DataFrame(data, columns=['A'])
print(df)
medium
A. DataFrame cannot be created from list of lists
B. Data should be a dictionary, not list of lists
C. Missing import statement for pandas
D. The columns list length does not match data inner list length

Solution

  1. Step 1: Check data shape and columns length

    Each inner list has 2 elements, but columns list has only 1 name.
  2. Step 2: Understand pandas error

    pandas requires columns length to match number of elements per row.
  3. Final Answer:

    The columns list length does not match data inner list length -> Option D
  4. Quick Check:

    Columns count = data columns count [OK]
Hint: Columns count must match inner list length [OK]
Common Mistakes:
  • Ignoring mismatch between columns and data
  • Thinking list of lists is invalid input
  • Forgetting to import pandas
5. You have sales data as a list of lists: sales = [[100, 200], [150, 250], [300, 400]] representing sales in two regions. How do you create a DataFrame with columns 'Region1' and 'Region2' and add a new column 'Total' that sums both regions for each row?
hard
A. df = pd.DataFrame(sales, columns=['Region1', 'Region2']) df['Total'] = sales[0] + sales[1] print(df)
B. df = pd.DataFrame(sales) df['Total'] = df[0] + df[1] print(df)
C. df = pd.DataFrame(sales, columns=['Region1', 'Region2']) df['Total'] = df['Region1'] + df['Region2'] print(df)
D. df = pd.DataFrame(sales, columns=['Region1', 'Region2']) df['Total'] = df.sum() print(df)

Solution

  1. Step 1: Create DataFrame with correct columns

    Use columns=['Region1', 'Region2'] to name columns properly.
  2. Step 2: Add 'Total' column by summing the two region columns

    Use df['Total'] = df['Region1'] + df['Region2'] to sum row-wise.
  3. Final Answer:

    df = pd.DataFrame(sales, columns=['Region1', 'Region2']) df['Total'] = df['Region1'] + df['Region2'] print(df) -> Option C
  4. Quick Check:

    Sum columns row-wise for total [OK]
Hint: Sum columns with df['Total'] = df['A'] + df['B'] [OK]
Common Mistakes:
  • Adding lists instead of DataFrame columns
  • Using df.sum() without axis=1 for row sums
  • Not naming columns when creating DataFrame
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard