pull
# https://dplyr.tidyverse.org/reference/pull.html
%run nb_helpers.py
from datar.data import starwars, mtcars
from datar.all import *
nb_header(pull)
★ pull¶
Pull a series or a dataframe from a dataframe¶
Args:¶
_data: The dataframe
var: The column to pull, either the name or the index
name: The name of the pulled value
- If to is frame, or the value pulled is data frame, it will be
the column names
- If to is series, it will be the series name. If multiple names
are given, only the first name will be used.
- If to is series, but value pulled is a data frame, then a
dictionary of series with the series names as keys or given name
as keys.
to: Type of data to return.
Only works when pulling a for name a$b
- series: Return a pandas Series object
Group information will be lost
If pulled value is a dataframe, it will return a dict of series,
with the series names or the name provided.
- array: Return a numpy.ndarray object
- frame: Return a DataFrame with that column
- list: Return a python list
- dict: Return a dict with name as keys and pulled value as values
Only a single column is allowed to pull
- If not provided: series when pulled data has only one columns.
dict if name provided and has the same length as the pulled
single column. Otherwise frame.
Returns:¶
The data according to to
mtcars >> pull(-1)
Mazda RX4 4 Mazda RX4 Wag 4 Datsun 710 1 Hornet 4 Drive 1 Hornet Sportabout 2 Valiant 1 Duster 360 4 Merc 240D 2 Merc 230 2 Merc 280 4 Merc 280C 4 Merc 450SE 3 Merc 450SL 3 Merc 450SLC 3 Cadillac Fleetwood 4 Lincoln Continental 4 Chrysler Imperial 4 Fiat 128 1 Honda Civic 2 Toyota Corolla 1 Toyota Corona 1 Dodge Challenger 2 AMC Javelin 2 Camaro Z28 4 Pontiac Firebird 2 Fiat X1-9 1 Porsche 914-2 2 Lotus Europa 2 Ford Pantera L 4 Ferrari Dino 6 Maserati Bora 8 Volvo 142E 2 Name: carb, dtype: int64
mtcars >> pull(-1, to='list')
[4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, 1, 1, 2, 2, 4, 2, 1, 2, 2, 4, 6, 8, 2]
mtcars >> pull(f.cyl)
Mazda RX4 6 Mazda RX4 Wag 6 Datsun 710 4 Hornet 4 Drive 6 Hornet Sportabout 8 Valiant 6 Duster 360 8 Merc 240D 4 Merc 230 4 Merc 280 6 Merc 280C 6 Merc 450SE 8 Merc 450SL 8 Merc 450SLC 8 Cadillac Fleetwood 8 Lincoln Continental 8 Chrysler Imperial 8 Fiat 128 4 Honda Civic 4 Toyota Corolla 4 Toyota Corona 4 Dodge Challenger 8 AMC Javelin 8 Camaro Z28 8 Pontiac Firebird 8 Fiat X1-9 4 Porsche 914-2 4 Lotus Europa 4 Ford Pantera L 8 Ferrari Dino 6 Maserati Bora 8 Volvo 142E 4 Name: cyl, dtype: int64
df = tibble(x=range(1, 11), y=range(10, 0, -1))
df >> mutate(z=f.x*f.y) >> pull()
0 10 1 18 2 24 3 28 4 30 5 30 6 28 7 24 8 18 9 10 Name: z, dtype: int64
starwars >> pull(f.height, name=f.name)
{'Luke Skywalker': 172.0,
'C-3PO': 167.0,
'R2-D2': 96.0,
'Darth Vader': 202.0,
'Leia Organa': 150.0,
'Owen Lars': 178.0,
'Beru Whitesun lars': 165.0,
'R5-D4': 97.0,
'Biggs Darklighter': 183.0,
'Obi-Wan Kenobi': 182.0,
'Anakin Skywalker': 188.0,
'Wilhuff Tarkin': 180.0,
'Chewbacca': 228.0,
'Han Solo': 180.0,
'Greedo': 173.0,
'Jabba Desilijic Tiure': 175.0,
'Wedge Antilles': 170.0,
'Jek Tono Porkins': 180.0,
'Yoda': 66.0,
'Palpatine': 170.0,
'Boba Fett': 183.0,
'IG-88': 200.0,
'Bossk': 190.0,
'Lando Calrissian': 177.0,
'Lobot': 175.0,
'Ackbar': 180.0,
'Mon Mothma': 150.0,
'Arvel Crynyd': nan,
'Wicket Systri Warrick': 88.0,
'Nien Nunb': 160.0,
'Qui-Gon Jinn': 193.0,
'Nute Gunray': 191.0,
'Finis Valorum': 170.0,
'Jar Jar Binks': 196.0,
'Roos Tarpals': 224.0,
'Rugor Nass': 206.0,
'Ric Olié': 183.0,
'Watto': 137.0,
'Sebulba': 112.0,
'Quarsh Panaka': 183.0,
'Shmi Skywalker': 163.0,
'Darth Maul': 175.0,
'Bib Fortuna': 180.0,
'Ayla Secura': 178.0,
'Dud Bolt': 94.0,
'Gasgano': 122.0,
'Ben Quadinaros': 163.0,
'Mace Windu': 188.0,
'Ki-Adi-Mundi': 198.0,
'Kit Fisto': 196.0,
'Eeth Koth': 171.0,
'Adi Gallia': 184.0,
'Saesee Tiin': 188.0,
'Yarael Poof': 264.0,
'Plo Koon': 188.0,
'Mas Amedda': 196.0,
'Gregar Typho': 185.0,
'Cordé': 157.0,
'Cliegg Lars': 183.0,
'Poggle the Lesser': 183.0,
'Luminara Unduli': 170.0,
'Barriss Offee': 166.0,
'Dormé': 165.0,
'Dooku': 193.0,
'Bail Prestor Organa': 191.0,
'Jango Fett': 183.0,
'Zam Wesell': 168.0,
'Dexter Jettster': 198.0,
'Lama Su': 229.0,
'Taun We': 213.0,
'Jocasta Nu': 167.0,
'Ratts Tyerell': 79.0,
'R4-P17': 96.0,
'Wat Tambor': 193.0,
'San Hill': 191.0,
'Shaak Ti': 178.0,
'Grievous': 216.0,
'Tarfful': 234.0,
'Raymus Antilles': 188.0,
'Sly Moore': 178.0,
'Tion Medon': 206.0,
'Finn': nan,
'Rey': nan,
'Poe Dameron': nan,
'BB8': nan,
'Captain Phasma': nan,
'Padmé Amidala': 165.0}