filter
In [1]:
Copied!
# https://dplyr.tidyverse.org/reference/filter.html
%run nb_helpers.py
from datar.data import starwars
from datar.all import *
nb_header(filter)
# https://dplyr.tidyverse.org/reference/filter.html
%run nb_helpers.py
from datar.data import starwars
from datar.all import *
nb_header(filter)
Try this notebook on binder.
★ filter_¶
Filter a data frame based on conditions¶
The original API:
https://dplyr.tidyverse.org/reference/filter.html
Args:¶
_data
: A data frame
*conditions
: Conditions to filter by.
_preserve
: If True
, keep grouping variables even if they are not used.
Returns:¶
The subset dataframe
In [2]:
Copied!
filter(starwars, f.species == "Human")
filter(starwars, f.species == "Human")
Out[2]:
name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
0 | Luke Skywalker | 172.0 | 77.0 | blond | fair | blue | 19.0 | male | masculine | Tatooine | Human |
3 | Darth Vader | 202.0 | 136.0 | none | white | yellow | 41.9 | male | masculine | Tatooine | Human |
4 | Leia Organa | 150.0 | 49.0 | brown | light | brown | 19.0 | female | feminine | Alderaan | Human |
5 | Owen Lars | 178.0 | 120.0 | brown, grey | light | blue | 52.0 | male | masculine | Tatooine | Human |
6 | Beru Whitesun lars | 165.0 | 75.0 | brown | light | blue | 47.0 | female | feminine | Tatooine | Human |
8 | Biggs Darklighter | 183.0 | 84.0 | black | light | brown | 24.0 | male | masculine | Tatooine | Human |
9 | Obi-Wan Kenobi | 182.0 | 77.0 | auburn, white | fair | blue-gray | 57.0 | male | masculine | Stewjon | Human |
10 | Anakin Skywalker | 188.0 | 84.0 | blond | fair | blue | 41.9 | male | masculine | Tatooine | Human |
11 | Wilhuff Tarkin | 180.0 | NaN | auburn, grey | fair | blue | 64.0 | male | masculine | Eriadu | Human |
13 | Han Solo | 180.0 | 80.0 | brown | fair | brown | 29.0 | male | masculine | Corellia | Human |
16 | Wedge Antilles | 170.0 | 77.0 | brown | fair | hazel | 21.0 | male | masculine | Corellia | Human |
17 | Jek Tono Porkins | 180.0 | 110.0 | brown | fair | blue | NaN | male | masculine | Bestine IV | Human |
19 | Palpatine | 170.0 | 75.0 | grey | pale | yellow | 82.0 | male | masculine | Naboo | Human |
20 | Boba Fett | 183.0 | 78.2 | black | fair | brown | 31.5 | male | masculine | Kamino | Human |
23 | Lando Calrissian | 177.0 | 79.0 | black | dark | brown | 31.0 | male | masculine | Socorro | Human |
24 | Lobot | 175.0 | 79.0 | none | light | blue | 37.0 | male | masculine | Bespin | Human |
26 | Mon Mothma | 150.0 | NaN | auburn | fair | blue | 48.0 | female | feminine | Chandrila | Human |
27 | Arvel Crynyd | NaN | NaN | brown | fair | brown | NaN | male | masculine | NaN | Human |
30 | Qui-Gon Jinn | 193.0 | 89.0 | brown | fair | blue | 92.0 | male | masculine | NaN | Human |
32 | Finis Valorum | 170.0 | NaN | blond | fair | blue | 91.0 | male | masculine | Coruscant | Human |
40 | Shmi Skywalker | 163.0 | NaN | black | fair | brown | 72.0 | female | feminine | Tatooine | Human |
47 | Mace Windu | 188.0 | 84.0 | none | dark | brown | 72.0 | male | masculine | Haruun Kal | Human |
56 | Gregar Typho | 185.0 | 85.0 | black | dark | brown | NaN | male | masculine | Naboo | Human |
57 | Cordé | 157.0 | NaN | brown | light | brown | NaN | female | feminine | Naboo | Human |
58 | Cliegg Lars | 183.0 | NaN | brown | fair | blue | 82.0 | male | masculine | Tatooine | Human |
62 | Dormé | 165.0 | NaN | brown | light | brown | NaN | female | feminine | Naboo | Human |
63 | Dooku | 193.0 | 80.0 | white | fair | brown | 102.0 | male | masculine | Serenno | Human |
64 | Bail Prestor Organa | 191.0 | NaN | black | tan | brown | 67.0 | male | masculine | Alderaan | Human |
65 | Jango Fett | 183.0 | 79.0 | black | tan | brown | 66.0 | male | masculine | Concord Dawn | Human |
70 | Jocasta Nu | 167.0 | NaN | white | fair | blue | NaN | female | feminine | Coruscant | Human |
78 | Raymus Antilles | 188.0 | 79.0 | brown | light | brown | NaN | male | masculine | Alderaan | Human |
81 | Finn | NaN | NaN | black | dark | dark | NaN | male | masculine | NaN | Human |
82 | Rey | NaN | NaN | brown | light | hazel | NaN | female | feminine | NaN | Human |
83 | Poe Dameron | NaN | NaN | brown | light | brown | NaN | male | masculine | NaN | Human |
86 | Padmé Amidala | 165.0 | 45.0 | brown | light | brown | 46.0 | female | feminine | Naboo | Human |
In [3]:
Copied!
filter(starwars, f.mass > 1000)
filter(starwars, f.mass > 1000)
Out[3]:
name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
15 | Jabba Desilijic Tiure | 175.0 | 1358.0 | NaN | green-tan, brown | orange | 600.0 | hermaphroditic | masculine | Nal Hutta | Hutt |
In [4]:
Copied!
# Conditions connected by & or | have to wrapped, since == has lower priority than & does
filter(starwars, (f.hair_color == "none") & (f.eye_color == "black"))
# Conditions connected by & or | have to wrapped, since == has lower priority than & does
filter(starwars, (f.hair_color == "none") & (f.eye_color == "black"))
Out[4]:
name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
29 | Nien Nunb | 160.0 | 68.0 | none | grey | black | NaN | male | masculine | Sullust | Sullustan |
45 | Gasgano | 122.0 | NaN | none | white, blue | black | NaN | male | masculine | Troiken | Xexto |
49 | Kit Fisto | 196.0 | 87.0 | none | green | black | NaN | male | masculine | Glee Anselm | Nautolan |
54 | Plo Koon | 188.0 | 80.0 | none | orange | black | 22.0 | male | masculine | Dorin | Kel Dor |
68 | Lama Su | 229.0 | 88.0 | none | grey | black | NaN | male | masculine | Kamino | Kaminoan |
69 | Taun We | 213.0 | NaN | none | grey | black | NaN | female | feminine | Kamino | Kaminoan |
75 | Shaak Ti | 178.0 | 57.0 | none | red, blue, white | black | NaN | female | feminine | Shili | Togruta |
80 | Tion Medon | 206.0 | 80.0 | none | grey | black | NaN | male | masculine | Utapau | Pau'an |
84 | BB8 | NaN | NaN | none | none | black | NaN | none | masculine | NaN | Droid |
In [5]:
Copied!
filter(starwars, (f.hair_color == "none") | (f.eye_color == "black"))
filter(starwars, (f.hair_color == "none") | (f.eye_color == "black"))
Out[5]:
name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
3 | Darth Vader | 202.0 | 136.0 | none | white | yellow | 41.9 | male | masculine | Tatooine | Human |
14 | Greedo | 173.0 | 74.0 | NaN | green | black | 44.0 | male | masculine | Rodia | Rodian |
21 | IG-88 | 200.0 | 140.0 | none | metal | red | 15.0 | none | masculine | NaN | Droid |
22 | Bossk | 190.0 | 113.0 | none | green | red | 53.0 | male | masculine | Trandosha | Trandoshan |
24 | Lobot | 175.0 | 79.0 | none | light | blue | 37.0 | male | masculine | Bespin | Human |
25 | Ackbar | 180.0 | 83.0 | none | brown mottle | orange | 41.0 | male | masculine | Mon Cala | Mon Calamari |
29 | Nien Nunb | 160.0 | 68.0 | none | grey | black | NaN | male | masculine | Sullust | Sullustan |
31 | Nute Gunray | 191.0 | 90.0 | none | mottled green | red | NaN | male | masculine | Cato Neimoidia | Neimodian |
33 | Jar Jar Binks | 196.0 | 66.0 | none | orange | orange | 52.0 | male | masculine | Naboo | Gungan |
34 | Roos Tarpals | 224.0 | 82.0 | none | grey | orange | NaN | male | masculine | Naboo | Gungan |
35 | Rugor Nass | 206.0 | NaN | none | green | orange | NaN | male | masculine | Naboo | Gungan |
38 | Sebulba | 112.0 | 40.0 | none | grey, red | orange | NaN | male | masculine | Malastare | Dug |
41 | Darth Maul | 175.0 | 80.0 | none | red | yellow | 54.0 | male | masculine | Dathomir | Zabrak |
42 | Bib Fortuna | 180.0 | NaN | none | pale | pink | NaN | male | masculine | Ryloth | Twi'lek |
43 | Ayla Secura | 178.0 | 55.0 | none | blue | hazel | 48.0 | female | feminine | Ryloth | Twi'lek |
44 | Dud Bolt | 94.0 | 45.0 | none | blue, grey | yellow | NaN | male | masculine | Vulpter | Vulptereen |
45 | Gasgano | 122.0 | NaN | none | white, blue | black | NaN | male | masculine | Troiken | Xexto |
46 | Ben Quadinaros | 163.0 | 65.0 | none | grey, green, yellow | orange | NaN | male | masculine | Tund | Toong |
47 | Mace Windu | 188.0 | 84.0 | none | dark | brown | 72.0 | male | masculine | Haruun Kal | Human |
49 | Kit Fisto | 196.0 | 87.0 | none | green | black | NaN | male | masculine | Glee Anselm | Nautolan |
51 | Adi Gallia | 184.0 | 50.0 | none | dark | blue | NaN | female | feminine | Coruscant | Tholothian |
52 | Saesee Tiin | 188.0 | NaN | none | pale | orange | NaN | male | masculine | Iktotch | Iktotchi |
53 | Yarael Poof | 264.0 | NaN | none | white | yellow | NaN | male | masculine | Quermia | Quermian |
54 | Plo Koon | 188.0 | 80.0 | none | orange | black | 22.0 | male | masculine | Dorin | Kel Dor |
55 | Mas Amedda | 196.0 | NaN | none | blue | blue | NaN | male | masculine | Champala | Chagrian |
59 | Poggle the Lesser | 183.0 | 80.0 | none | green | yellow | NaN | male | masculine | Geonosis | Geonosian |
67 | Dexter Jettster | 198.0 | 102.0 | none | brown | yellow | NaN | male | masculine | Ojom | Besalisk |
68 | Lama Su | 229.0 | 88.0 | none | grey | black | NaN | male | masculine | Kamino | Kaminoan |
69 | Taun We | 213.0 | NaN | none | grey | black | NaN | female | feminine | Kamino | Kaminoan |
71 | Ratts Tyerell | 79.0 | 15.0 | none | grey, blue | unknown | NaN | male | masculine | Aleen Minor | Aleena |
72 | R4-P17 | 96.0 | NaN | none | silver, red | red, blue | NaN | none | feminine | NaN | Droid |
73 | Wat Tambor | 193.0 | 48.0 | none | green, grey | unknown | NaN | male | masculine | Skako | Skakoan |
74 | San Hill | 191.0 | NaN | none | grey | gold | NaN | male | masculine | Muunilinst | Muun |
75 | Shaak Ti | 178.0 | 57.0 | none | red, blue, white | black | NaN | female | feminine | Shili | Togruta |
76 | Grievous | 216.0 | 159.0 | none | brown, white | green, yellow | NaN | male | masculine | Kalee | Kaleesh |
79 | Sly Moore | 178.0 | 48.0 | none | pale | white | NaN | NaN | NaN | Umbara | NaN |
80 | Tion Medon | 206.0 | 80.0 | none | grey | black | NaN | male | masculine | Utapau | Pau'an |
84 | BB8 | NaN | NaN | none | none | black | NaN | none | masculine | NaN | Droid |
In [6]:
Copied!
filter(starwars, f.hair_color == "none", f.eye_color == "black")
filter(starwars, f.hair_color == "none", f.eye_color == "black")
Out[6]:
name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
29 | Nien Nunb | 160.0 | 68.0 | none | grey | black | NaN | male | masculine | Sullust | Sullustan |
45 | Gasgano | 122.0 | NaN | none | white, blue | black | NaN | male | masculine | Troiken | Xexto |
49 | Kit Fisto | 196.0 | 87.0 | none | green | black | NaN | male | masculine | Glee Anselm | Nautolan |
54 | Plo Koon | 188.0 | 80.0 | none | orange | black | 22.0 | male | masculine | Dorin | Kel Dor |
68 | Lama Su | 229.0 | 88.0 | none | grey | black | NaN | male | masculine | Kamino | Kaminoan |
69 | Taun We | 213.0 | NaN | none | grey | black | NaN | female | feminine | Kamino | Kaminoan |
75 | Shaak Ti | 178.0 | 57.0 | none | red, blue, white | black | NaN | female | feminine | Shili | Togruta |
80 | Tion Medon | 206.0 | 80.0 | none | grey | black | NaN | male | masculine | Utapau | Pau'an |
84 | BB8 | NaN | NaN | none | none | black | NaN | none | masculine | NaN | Droid |
In [8]:
Copied!
starwars >> filter(f.mass > mean(f.mass))
starwars >> filter(f.mass > mean(f.mass))
Out[8]:
name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
3 | Darth Vader | 202.0 | 136.0 | none | white | yellow | 41.9 | male | masculine | Tatooine | Human |
5 | Owen Lars | 178.0 | 120.0 | brown, grey | light | blue | 52.0 | male | masculine | Tatooine | Human |
12 | Chewbacca | 228.0 | 112.0 | brown | unknown | blue | 200.0 | male | masculine | Kashyyyk | Wookiee |
15 | Jabba Desilijic Tiure | 175.0 | 1358.0 | NaN | green-tan, brown | orange | 600.0 | hermaphroditic | masculine | Nal Hutta | Hutt |
17 | Jek Tono Porkins | 180.0 | 110.0 | brown | fair | blue | NaN | male | masculine | Bestine IV | Human |
21 | IG-88 | 200.0 | 140.0 | none | metal | red | 15.0 | none | masculine | NaN | Droid |
22 | Bossk | 190.0 | 113.0 | none | green | red | 53.0 | male | masculine | Trandosha | Trandoshan |
67 | Dexter Jettster | 198.0 | 102.0 | none | brown | yellow | NaN | male | masculine | Ojom | Besalisk |
76 | Grievous | 216.0 | 159.0 | none | brown, white | green, yellow | NaN | male | masculine | Kalee | Kaleesh |
77 | Tarfful | 234.0 | 136.0 | brown | brown | blue | NaN | male | masculine | Kashyyyk | Wookiee |
In [ ]:
Copied!
starwars >> group_by(f.gender) >> filter(f.mass > mean(f.mass, na_rm=True))
starwars >> group_by(f.gender) >> filter(f.mass > mean(f.mass, na_rm=True))
Out[ ]:
name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
0 | Darth Vader | 202.0 | 136.0 | none | white | yellow | 41.9 | male | masculine | Tatooine | Human |
1 | Owen Lars | 178.0 | 120.0 | brown, grey | light | blue | 52.0 | male | masculine | Tatooine | Human |
2 | Beru Whitesun lars | 165.0 | 75.0 | brown | light | blue | 47.0 | female | feminine | Tatooine | Human |
3 | Chewbacca | 228.0 | 112.0 | brown | unknown | blue | 200.0 | male | masculine | Kashyyyk | Wookiee |
4 | Jabba Desilijic Tiure | 175.0 | 1358.0 | NaN | green-tan, brown | orange | 600.0 | hermaphroditic | masculine | Nal Hutta | Hutt |
5 | Jek Tono Porkins | 180.0 | 110.0 | brown | fair | blue | NaN | male | masculine | Bestine IV | Human |
6 | IG-88 | 200.0 | 140.0 | none | metal | red | 15.0 | none | masculine | NaN | Droid |
7 | Bossk | 190.0 | 113.0 | none | green | red | 53.0 | male | masculine | Trandosha | Trandoshan |
8 | Ayla Secura | 178.0 | 55.0 | none | blue | hazel | 48.0 | female | feminine | Ryloth | Twi'lek |
9 | Luminara Unduli | 170.0 | 56.2 | black | yellow | blue | 58.0 | female | feminine | Mirial | Mirialan |
10 | Zam Wesell | 168.0 | 55.0 | blonde | fair, green, yellow | yellow | NaN | female | feminine | Zolan | Clawdite |
11 | Shaak Ti | 178.0 | 57.0 | none | red, blue, white | black | NaN | female | feminine | Shili | Togruta |
12 | Grievous | 216.0 | 159.0 | none | brown, white | green, yellow | NaN | male | masculine | Kalee | Kaleesh |
13 | Tarfful | 234.0 | 136.0 | brown | brown | blue | NaN | male | masculine | Kashyyyk | Wookiee |
TibbleGrouped: gender (n=2)
In [ ]:
Copied!
# To refer to column names that are stored as strings, use `f[...]`:
vars = c("mass", "height")
cond = c(80, 150)
starwars >> \
filter(
f[vars[0]] > cond[0],
f[vars[1]] > cond[1]
)
# To refer to column names that are stored as strings, use `f[...]`:
vars = c("mass", "height")
cond = c(80, 150)
starwars >> \
filter(
f[vars[0]] > cond[0],
f[vars[1]] > cond[1]
)
Out[ ]:
name | height | mass | hair_color | skin_color | eye_color | birth_year | sex | gender | homeworld | species | |
---|---|---|---|---|---|---|---|---|---|---|---|
<object> | <float64> | <float64> | <object> | <object> | <object> | <float64> | <object> | <object> | <object> | <object> | |
3 | Darth Vader | 202.0 | 136.0 | none | white | yellow | 41.9 | male | masculine | Tatooine | Human |
5 | Owen Lars | 178.0 | 120.0 | brown, grey | light | blue | 52.0 | male | masculine | Tatooine | Human |
8 | Biggs Darklighter | 183.0 | 84.0 | black | light | brown | 24.0 | male | masculine | Tatooine | Human |
10 | Anakin Skywalker | 188.0 | 84.0 | blond | fair | blue | 41.9 | male | masculine | Tatooine | Human |
12 | Chewbacca | 228.0 | 112.0 | brown | unknown | blue | 200.0 | male | masculine | Kashyyyk | Wookiee |
15 | Jabba Desilijic Tiure | 175.0 | 1358.0 | NaN | green-tan, brown | orange | 600.0 | hermaphroditic | masculine | Nal Hutta | Hutt |
17 | Jek Tono Porkins | 180.0 | 110.0 | brown | fair | blue | NaN | male | masculine | Bestine IV | Human |
21 | IG-88 | 200.0 | 140.0 | none | metal | red | 15.0 | none | masculine | NaN | Droid |
22 | Bossk | 190.0 | 113.0 | none | green | red | 53.0 | male | masculine | Trandosha | Trandoshan |
25 | Ackbar | 180.0 | 83.0 | none | brown mottle | orange | 41.0 | male | masculine | Mon Cala | Mon Calamari |
30 | Qui-Gon Jinn | 193.0 | 89.0 | brown | fair | blue | 92.0 | male | masculine | NaN | Human |
31 | Nute Gunray | 191.0 | 90.0 | none | mottled green | red | NaN | male | masculine | Cato Neimoidia | Neimodian |
34 | Roos Tarpals | 224.0 | 82.0 | none | grey | orange | NaN | male | masculine | Naboo | Gungan |
47 | Mace Windu | 188.0 | 84.0 | none | dark | brown | 72.0 | male | masculine | Haruun Kal | Human |
48 | Ki-Adi-Mundi | 198.0 | 82.0 | white | pale | yellow | 92.0 | male | masculine | Cerea | Cerean |
49 | Kit Fisto | 196.0 | 87.0 | none | green | black | NaN | male | masculine | Glee Anselm | Nautolan |
56 | Gregar Typho | 185.0 | 85.0 | black | dark | brown | NaN | male | masculine | Naboo | Human |
67 | Dexter Jettster | 198.0 | 102.0 | none | brown | yellow | NaN | male | masculine | Ojom | Besalisk |
68 | Lama Su | 229.0 | 88.0 | none | grey | black | NaN | male | masculine | Kamino | Kaminoan |
76 | Grievous | 216.0 | 159.0 | none | brown, white | green, yellow | NaN | male | masculine | Kalee | Kaleesh |
77 | Tarfful | 234.0 | 136.0 | brown | brown | blue | NaN | male | masculine | Kashyyyk | Wookiee |