0
0
Pandasdata~10 mins

Why DataFrame creation matters in Pandas - Test Your Understanding

Choose your learning style9 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