Have a go at it. The editor and the docs panel are open, and your code is saved as you type. Running it needs a free account — you’ll come back to exactly what you wrote.
Building a DataFrame
A DataFrame is a table: named columns, numbered rows. Where a NumPy array is one grid of one type, a DataFrame can hold a text column next to a number column, and you address columns by name rather than by position.
import pandas as pd
df = pd.DataFrame({
"name": ["ada", "grace"],
"score": [90, 85],
})That dictionary is read as "column name -> the values down that column". Each list becomes one column and they must all be the same length.
The things you will do to every new frame, in this order:
df.shape # (2, 2) (rows, columns) df.columns # the column names df.head() # the first 5 rows df.dtypes # what type each column ended up as
df.dtypes is the one people skip and should not. A column that came out as object when you expected a number means something non-numeric got in, and every calculation on it will either fail or be quietly wrong.
Your task:
Write build_frame(names, scores) that returns a DataFrame with a "name" column and a "score" column, then returns its shape as a list.
The test checks the shape, so return the DataFrame and let the harness read .shape off it.
Example Tests
Two people gives a 2 by 2 table
Input: {"names":["ada","grace"],"scores":[90,85]}
Expected: [2,2]
Three people gives three rows and still two columns
Input: {"names":["a","b","c"],"scores":[1,2,3]}
Expected: [3,2]
The score column holds the values you passed in
Input: {"names":["a","b"],"scores":[7,8]}
Expected: 15
import pandas as pd
def build_frame(names, scores):
"""
Build a two-column DataFrame.
Args:
names: a list of strings
scores: a list of numbers, the same length
Returns:
A DataFrame with columns "name" and "score".
"""
# YOUR CODE HERE
pass
def _score_total(names, scores):
"""Total of the score column of the frame that was built."""
return float(build_frame(names, scores)["score"].sum())Run your code to see results
⌘↵ runs against the visible tests