BBampaa

Lessons › Python

Python lesson

Dropping rows and columns

3 complete orders are left. The order matters: run .dropna() first and the empty Notes would throw away O403 too, a good order, leaving only 2.

Try this lesson free →
About 5–10 minutes · no sign-up needed

Example

.drop(columns=[...]) removes columns. .dropna() removes every row that has a blank anywhere:

clean = df.drop(columns=["Notes"]).dropna()
clean

Output:

Order Store Revenue
0 O401 Lagos 70
2 O403 Lagos 35
4 O405 Abuja 140

Key points

.drop(columns=["Notes"])Remove the columns in the list.
.dropna()Remove every row with a blank anywhere in it.
Order mattersDrop mostly-empty columns first, or .dropna() throws away good rows.
Save it backdf = df.dropna(). On its own line, .dropna() only shows the result.

Drop columns with .drop(columns=[...]), then blank rows with .dropna(). Columns first.

Practice

Try each one in your head first, then open the answer.

1. Build a line that removes the Notes column.
df.drop(columns=["Notes"])
2. Build a line that removes Notes, then every row with a blank.
df.drop(columns=["Notes"]).dropna()
3. Remove the Notes column and show the table.
df.drop(columns=["Notes"])
4. Remove the Notes column, then every row with a blank, and save the result back into df. Show df.
df = df.drop(columns=["Notes"]).dropna()
df
5. Remove only the rows with a blank, keeping every column, and show how many rows are left.
len(df.dropna())

Learn it by doing. This lesson is a short hands-on level on Bampaa: see it work, then build and write it yourself, with instant feedback.

Try this lesson free →
About 5–10 minutes · no sign-up needed