Start learning free

LESSON 10 OF 165 · MACHINE LEARNING: PANDAS

Columns and Their Types

Practice

A DataFrame is made of columns. Each column has a name and holds one type of value. df.columns holds the names in order, and list(df.columns) turns them into a plain Python list. df.dtypes gives back one type per column: int64 for whole numbers, float64 for decimals and str for text. A column with blank cells (NaN) becomes float64, because NaN is a decimal value: that's why ages print as 52.0.

Pick one column by its name in square brackets and you get a Series: one column of values, each with its row label. A Series has methods that sum it up: .mean() gives back the average, .max() the biggest value and .min() the smallest. On the visits column:

visits = df["visits"]
print(visits.min())
print(round(visits.mean(), 1))

.describe() gives back 8 numbers at once: count (values that aren't blank), mean, std (how far the values spread from the mean), min, the 25%, 50% and 75% marks, and max. The 50% mark is the median: half the values are at or below it. round(x, 2) rounds one number; .round(2) rounds every number in a Series, and .to_string() prints a Series as plain lines.

Your script must print this (the first line is cut here with …):

['customer_id', 'city', 'age', 'joined', 'newsletter', 'visits', …]
customer_id          int64
city                   str
age                float64
joined                 str
newsletter             str
visits               int64
orders               int64
spent                int64
days_since_last      int64
returned_item        int64
bought_again         int64
<class 'pandas.Series'>
Mean: 189.93
Max: 720
count    300.00
mean     189.93
std      151.52
min        0.00
25%       72.75
50%      158.00
75%      270.25
max      720.00

Your task: below the line that reads customers.csv, print the column names as a list and the types with .to_string(). Then print the type of df["spent"], "Mean:" with its mean rounded to 2 decimals, "Max:" with its biggest value, and its describe() rounded to 2 decimals, with .to_string().


Assignment
Follow the Coding Tutorial to list the table's columns and their types, then summarize the spent column.


Hint
df.columns and df.dtypes have no parentheses: they are values. mean(), max() and describe() are methods, so they need them. round(..., 2) wraps one number; .round(2) goes after a Series.


STEP 1 OF 6
    Ctrl + Enter to check