Building a DataFrame

A table is a dictionary of columns that share one index.

From a dictionary of columns

The keys become column names and the values become the columns. Every column shares the same index, which is what makes it a table rather than a bag of Series.

import pandas as pd

df = pd.DataFrame({
    "name": ["Ada", "Grace", "Alan"],
    "score": [92, 88, 79],
})
print(df)
print(df.shape)

The first four things to ask it

shape, columns, head and info answer "what am I holding" before you do anything else. Skipping this step is how people summarise a column that is not what they think it is.

import pandas as pd

df = pd.DataFrame({"name": ["Ada"], "score": [92]})
print(df.shape)
print(list(df.columns))
print(df.head())

Exercise

Try It Yourself

Write describe_table(df) that returns a tuple of the number of rows, the number of columns, and the column names as a list.

Press Run to see output

Check Your Understanding