pd.DataFrame.filter
is a specialized method that allows you to select from either the rows or columns of a pd.DataFrame
.
How to do it
Let’s create a pd.DataFrame
where we have indices composed of strings in both the rows and columns:
df = pd.DataFrame([
[24, 180, "blue"],
[42, 166, "brown"],
[22, 160, "green"],
], columns=[
"age",
"height_cm",
"eye_color"
], index=["Jack", "Jill", "Jayne"])
df
age height_cm eye_color
Jack 24 180 blue
Jill 42 166 brown
Jayne 22 160 green
By default, pd.DataFrame.filter
will select columns matching the label argument(s), similar to pd.DataFrame[]
:
df.filter(["age", "eye_color"])
age eye_color
Jack 24 blue
Jill 42 brown
Jayne 22 green
However, pd.DataFrame.filter
also accepts an axis=
...