Bird
Raised Fist0
Pandasdata~10 mins

Why DataFrame creation matters in Pandas - Test Your Understanding

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

Complete the code to create a DataFrame from a dictionary.

Pandas
import pandas as pd
data = {'name': ['Anna', 'Bob'], 'age': [25, 30]}
df = pd.[1](data)
print(df)
Drag options to blanks, or click blank then click option'
ADataFrame
BSeries
Carray
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using pd.Series instead of pd.DataFrame
Trying to create a DataFrame with pd.list or pd.array which do not exist
2fill in blank
medium

Complete the code to create a DataFrame with specified columns order.

Pandas
import pandas as pd
data = {'name': ['Anna', 'Bob'], 'age': [25, 30]}
df = pd.DataFrame(data, columns=[1])
print(df)
Drag options to blanks, or click blank then click option'
A['age', 'name']
B['name', 'age']
C['age']
D['name']
Attempts:
3 left
💡 Hint
Common Mistakes
Reversing the order of columns
Passing a single string instead of a list
3fill in blank
hard

Fix the error in the code to create a DataFrame from a list of lists.

Pandas
import pandas as pd
data = [['Anna', 25], ['Bob', 30]]
df = pd.DataFrame([1], columns=['name', 'age'])
print(df)
Drag options to blanks, or click blank then click option'
Adata()
B{data}
C['data']
Ddata
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces around the variable
Passing the variable as a string
Trying to call the variable as a function
4fill in blank
hard

Fill both blanks to create a DataFrame from a list of dictionaries and set the index.

Pandas
import pandas as pd
data = [{'name': 'Anna', 'age': 25}, {'name': 'Bob', 'age': 30}]
df = pd.DataFrame([1])
df = df.set_index([2])
print(df)
Drag options to blanks, or click blank then click option'
Adata
B'name'
C'age'
D['name']
Attempts:
3 left
💡 Hint
Common Mistakes
Using a list instead of a string for the index column
Setting index to 'age' which may not be unique
Passing data incorrectly
5fill in blank
hard

Fill all three blanks to create a DataFrame from a dictionary, rename columns, and select a subset.

Pandas
import pandas as pd
data = {'n': ['Anna', 'Bob'], 'a': [25, 30], 'c': ['NY', 'LA']}
df = pd.DataFrame([1])
df = df.rename(columns=[2])
df_subset = df.loc[:, [3]]
print(df_subset)
Drag options to blanks, or click blank then click option'
Adata
B{'n': 'name', 'a': 'age', 'c': 'city'}
C['name', 'age']
D['n', 'a']
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong variable names
Passing rename columns as a list instead of a dictionary
Selecting columns by old names after renaming

Practice

(1/5)
1. Why is creating a DataFrame important when working with data in pandas?
easy
A. It converts data into images.
B. It automatically cleans all data errors.
C. It organizes data into rows and columns for easy analysis.
D. It deletes duplicate data without user input.

Solution

  1. Step 1: Understand DataFrame structure

    A DataFrame organizes data into rows and columns, similar to a spreadsheet.
  2. Step 2: Recognize the benefit for analysis

    This structure makes it easier to analyze and manipulate data clearly and simply.
  3. Final Answer:

    It organizes data into rows and columns for easy analysis. -> Option C
  4. Quick Check:

    DataFrames = organized data [OK]
Hint: DataFrames = tables that make data easy to handle [OK]
Common Mistakes:
  • Thinking DataFrames fix data errors automatically
  • Confusing DataFrames with image data
  • Assuming DataFrames delete duplicates by default
2. Which of the following is the correct way to create a pandas DataFrame from a dictionary?
easy
A. pd.DataFrame({'A': [1, 2], 'B': [3, 4]})
B. pd.DataFrame['A': [1, 2], 'B': [3, 4]]
C. pd.DataFrame({'A' = [1, 2], 'B' = [3, 4]})
D. pd.DataFrame({'A': 1, 2, 'B': 3, 4})

Solution

  1. Step 1: Check dictionary syntax

    The dictionary keys must be strings and values lists, using colons ':' to assign.
  2. Step 2: Confirm DataFrame constructor usage

    pd.DataFrame() takes a dictionary with keys as column names and lists as column data.
  3. Final Answer:

    pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) -> Option A
  4. Quick Check:

    Correct dict + pd.DataFrame() = pd.DataFrame({'A': [1, 2], 'B': [3, 4]}) [OK]
Hint: Use curly braces and colons inside pd.DataFrame() [OK]
Common Mistakes:
  • Using square brackets instead of parentheses for DataFrame
  • Using '=' instead of ':' in dictionary
  • Passing single values instead of lists for column data
3. What will be the output of this code?
import pandas as pd
data = {'Name': ['Anna', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df.shape)
medium
A. (2, 3)
B. (2, 2)
C. (3, 2)
D. (1, 2)

Solution

  1. Step 1: Understand DataFrame shape

    DataFrame shape returns (rows, columns).
  2. Step 2: Count rows and columns in data

    Data has 2 names and 2 ages, so 2 rows and 2 columns.
  3. Final Answer:

    (2, 2) -> Option B
  4. Quick Check:

    Rows=2, Columns=2 [OK]
Hint: Shape = (rows, columns) count [OK]
Common Mistakes:
  • Confusing rows with columns count
  • Assuming shape returns columns first
  • Counting keys as rows
4. Identify the error in this DataFrame creation code:
import pandas as pd
data = {'X': [1, 2], 'Y': 3}
df = pd.DataFrame(data)
medium
A. Data values must be lists or arrays, not single integers.
B. Dictionary keys must be integers, not strings.
C. pd.DataFrame() cannot take a dictionary as input.
D. Missing import statement for pandas.

Solution

  1. Step 1: Check data dictionary values

    Values are single integers, but DataFrame expects list-like objects for columns.
  2. Step 2: Understand DataFrame input requirements

    Each column must have multiple values in a list or array to form rows.
  3. Final Answer:

    Data values must be lists or arrays, not single integers. -> Option A
  4. Quick Check:

    Values need to be lists [OK]
Hint: Use lists for column data, not single numbers [OK]
Common Mistakes:
  • Using single values instead of lists
  • Thinking keys must be integers
  • Ignoring import pandas statement
5. You have nested data: {'Team': ['A', 'B'], 'Scores': [[10, 20], [15, 25]]}. How do you create a DataFrame that shows each score as a separate row with the team name repeated?
hard
A. Convert nested lists to strings before creating the DataFrame.
B. Create DataFrame directly with nested lists; pandas will flatten automatically.
C. Use pd.DataFrame({'Team': ['A', 'B'], 'Scores': [10, 20, 15, 25]}).
D. Use pd.DataFrame.explode() on the 'Scores' column after creating the DataFrame.

Solution

  1. Step 1: Create DataFrame with nested lists

    Creating DataFrame with nested lists keeps 'Scores' as lists in cells.
  2. Step 2: Use explode() to flatten lists into rows

    explode() expands each list element into its own row, repeating 'Team' values.
  3. Final Answer:

    Use pd.DataFrame.explode() on the 'Scores' column after creating the DataFrame. -> Option D
  4. Quick Check:

    explode() flattens nested lists [OK]
Hint: Use explode() to turn list elements into rows [OK]
Common Mistakes:
  • Expecting pandas to flatten nested lists automatically
  • Manually flattening data without explode()
  • Converting lists to strings losing data structure
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