Start learning free

LESSON 12 OF 165 · MACHINE LEARNING: PANDAS

Filter Rows in Python

Practice

Most questions about data are about some of the rows: the customers who spent a lot, or the ones from two cities. In pandas you filter in two moves. First you ask a column a question, like df["visits"] > 20: it gives back a Series of True/False, one answer per row. Then df[...] with that Series inside gives back a new DataFrame with only the True rows.

regulars = df[df["visits"] > 20]
print(len(regulars))

The kept rows stay in their order and keep their labels from df, so the labels on the left can jump, like 0, 3, 52. len(table) gives back how many rows a table has.

To ask two questions at once, put each one in its own parentheses and join them: & keeps a row when both answers are True, | when at least one is, as in df[(df["orders"] > 5) & (df["returned_item"] == 0)]. Python's and and or don't work on whole columns. To ask "is the value one of these?", df["city"].isin(["Lyon", "Porto"]) gives back True for each row whose city is in the list: a short way to write many |.

Your script must print:

    customer_id  city  spent
0             1  Rome    720
3             4  Lyon    597
52           53  Lyon    505
Over 500: 16 with newsletter: 11
Madrid or Rome: 92 with isin: 92

Your task: df is already read from customers.csv. Keep the customers who spent over 500 in big and print 3 columns of its first 3 rows. Keep the ones who also have newsletter equal to "yes" in fans, with &. Keep the customers from Madrid or Rome twice, with | in either and with isin in listed, and print how many rows each table has.


Assignment
Follow the Coding Tutorial to filter the customers by spent, by spent and newsletter, and by city.


Hint
Each condition goes in its own parentheses before you join it with & or |: Python does & and | before > and ==.


STEP 1 OF 7
    Ctrl + Enter to check

    Pd Filter in Python

    Keep going with Python1,600+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free