0
0
Data Analysis Pythondata~20 mins

Creating DataFrames (dict, list, CSV) in Data Analysis Python - 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?
Consider the following Python code that creates a DataFrame from a dictionary. What will be the output when printing df?
Data Analysis Python
import pandas as pd

data = {'Name': ['Alice', 'Bob'], 'Age': [25, 30]}
df = pd.DataFrame(data)
print(df)
A
03   boB    1
52   ecilA  0
egA  emaN
B
   Name  Age
1  Alice   25
2    Bob   30
C
   Name  Age
0  Alice   25
1    Bob   30
D
   Name  Age
0  Alice  25
1  Bob  30
2  None  None
Attempts:
2 left
💡 Hint
Look at how pandas assigns default row indices when creating a DataFrame from a dictionary.
data_output
intermediate
1:30remaining
How many rows and columns does this DataFrame have?
Given the code below, how many rows and columns does the resulting DataFrame contain?
Data Analysis Python
import pandas as pd

list_of_dicts = [{'city': 'Paris', 'temp': 20}, {'city': 'Berlin', 'temp': 18}, {'city': 'Rome', 'temp': 25}]
df = pd.DataFrame(list_of_dicts)
print(df.shape)
A(3, 2)
B(3, 3)
C(2, 3)
D(2, 2)
Attempts:
2 left
💡 Hint
The shape attribute returns (rows, columns). Count the dictionaries and keys.
🔧 Debug
advanced
1:00remaining
What error does this code raise when reading a CSV?
What error will this code produce when trying to read the CSV file?
Data Analysis Python
import pandas as pd

df = pd.read_csv('nonexistent_file.csv')
AFileNotFoundError
BValueError
CKeyError
DTypeError
Attempts:
2 left
💡 Hint
Think about what happens if the file path does not exist.
🚀 Application
advanced
2:00remaining
Which code creates a DataFrame with columns 'A' and 'B' from a list of lists?
You have data as a list of lists: [[1, 2], [3, 4]]. Which code correctly creates a DataFrame with columns named 'A' and 'B'?
Apd.DataFrame([[1, 2], [3, 4]], index=['A', 'B'])
Bpd.DataFrame({'A': [1, 3], 'B': [2, 4]})
Cpd.DataFrame({'A': 1, 'B': 2}, [[3, 4]])
Dpd.DataFrame([[1, 2], [3, 4]], columns=['A', 'B'])
Attempts:
2 left
💡 Hint
When using a list of lists, specify columns with the columns parameter.
visualization
expert
2:30remaining
What does this code plot from a CSV file?
Given a CSV file 'data.csv' with columns 'Year' and 'Sales', what will the following code plot?
Data Analysis Python
import pandas as pd
import matplotlib.pyplot as plt

df = pd.read_csv('data.csv')
df.plot(x='Year', y='Sales', kind='line')
plt.show()
AA pie chart of Sales distribution
BA line chart showing Sales over Year
CA scatter plot of Year vs Sales
DA bar chart showing Sales over Year
Attempts:
2 left
💡 Hint
The kind='line' parameter specifies the plot type.