dr.slice_head() - Return the first n rows.
dr.slice_tail() - Return the last n rows.
dr.glimpse() - Transpose print of tibble, showing data types and a preview of data (like df.info())
In [1]:
Copied!
import datar.all as dr
from datar import f
import pandas as pd
from pathlib import Path
pd.set_option("display.width", 200)
# Get the path object pointing to the ``notebooks`` directory that contains *.csv files
data_dir = next(Path("/home").rglob("*/notebooks/*.csv")).parent
import datar.all as dr
from datar import f
import pandas as pd
from pathlib import Path
pd.set_option("display.width", 200)
# Get the path object pointing to the ``notebooks`` directory that contains *.csv files
data_dir = next(Path("/home").rglob("*/notebooks/*.csv")).parent
In [2]:
Copied!
tb_pokemon = dr.tibble(
pd.read_csv(data_dir/"pokemon.csv")
>> dr.rename_with(lambda col: col.strip().replace(" ", "_").replace(".", "")) # Clean column names
>> dr.select(~f["#"]) # Drop the "#" column
>> dr.mutate(
Type_1 = f.Type_1.astype("category"), # convert to category (pandas style)
Type_2 = dr.as_factor(f.Type_2), # convert to category (datar style)
Generation = dr.as_ordered(f.Generation), # convert to ordered category (datar style)
Legendary = dr.as_logical(f.Legendary) # convert to boolean (datar style)
)
)
tb_pokemon = dr.tibble(
pd.read_csv(data_dir/"pokemon.csv")
>> dr.rename_with(lambda col: col.strip().replace(" ", "_").replace(".", "")) # Clean column names
>> dr.select(~f["#"]) # Drop the "#" column
>> dr.mutate(
Type_1 = f.Type_1.astype("category"), # convert to category (pandas style)
Type_2 = dr.as_factor(f.Type_2), # convert to category (datar style)
Generation = dr.as_ordered(f.Generation), # convert to ordered category (datar style)
Legendary = dr.as_logical(f.Legendary) # convert to boolean (datar style)
)
)
1. dr.slice_head()¶
In [3]:
Copied!
##-----------------------##
## Show the first 3 rows ##
##-----------------------##
print(
tb_pokemon >> dr.slice_head(n=3)
)
##-----------------------##
## Show the first 3 rows ##
##-----------------------##
print(
tb_pokemon >> dr.slice_head(n=3)
)
Name Type_1 Type_2 Total HP Attack Defense Sp_Atk Sp_Def Speed Generation Legendary
<str> <category> <category> <int64> <int64> <int64> <int64> <int64> <int64> <int64> <category> <bool>
0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 False
1 Ivysaur Grass Poison 405 60 62 63 80 80 60 1 False
2 Venusaur Grass Poison 525 80 82 83 100 100 80 1 False
In [4]:
Copied!
##--------------------------------##
## Show the first 1 row (default) ##
##--------------------------------##
print(
tb_pokemon
>> dr.slice_head()
)
##--------------------------------##
## Show the first 1 row (default) ##
##--------------------------------##
print(
tb_pokemon
>> dr.slice_head()
)
Name Type_1 Type_2 Total HP Attack Defense Sp_Atk Sp_Def Speed Generation Legendary
<str> <category> <category> <int64> <int64> <int64> <int64> <int64> <int64> <int64> <category> <bool>
0 Bulbasaur Grass Poison 318 45 49 49 65 65 45 1 False
2. dr.slice_tail()¶
In [5]:
Copied!
##----------------------##
## Show the last 3 rows ##
##----------------------##
print(
tb_pokemon >> dr.slice_tail(n=3)
)
##----------------------##
## Show the last 3 rows ##
##----------------------##
print(
tb_pokemon >> dr.slice_tail(n=3)
)
Name Type_1 Type_2 Total HP Attack Defense Sp_Atk Sp_Def Speed Generation Legendary
<str> <category> <category> <int64> <int64> <int64> <int64> <int64> <int64> <int64> <category> <bool>
797 HoopaHoopa Confined Psychic Ghost 600 80 110 60 150 130 70 6 True
798 HoopaHoopa Unbound Psychic Dark 680 80 160 60 170 130 80 6 True
799 Volcanion Fire Water 600 80 110 120 130 90 70 6 True
In [6]:
Copied!
##--------------------------------##
## Show the last 1 rows (default) ##
##--------------------------------##
print(
tb_pokemon
>> dr.slice_tail()
)
##--------------------------------##
## Show the last 1 rows (default) ##
##--------------------------------##
print(
tb_pokemon
>> dr.slice_tail()
)
Name Type_1 Type_2 Total HP Attack Defense Sp_Atk Sp_Def Speed Generation Legendary
<str> <category> <category> <int64> <int64> <int64> <int64> <int64> <int64> <int64> <category> <bool>
799 Volcanion Fire Water 600 80 110 120 130 90 70 6 True
3. dr.glimpse()¶
In [8]:
Copied!
# Glimpse of the dataframe
'''
dr.glimpse(tb_pokemon) DOES NOT WORK, must use the pipe operator (>>)
'''
tb_pokemon >> dr.glimpse()
# Glimpse of the dataframe
'''
dr.glimpse(tb_pokemon) DOES NOT WORK, must use the pipe operator (>>)
'''
tb_pokemon >> dr.glimpse()
Out[8]:
Rows: 800
Columns: 12
| . Name | <str> | 'Bulbasaur', 'Ivysaur', 'Venusaur', 'VenusaurMega Venusaur', 'Charmander',… |
|---|---|---|
| . Type_1 | <category> | 'Grass', 'Grass', 'Grass', 'Grass', 'Fire', 'Fire', 'Fire', 'Fire', 'Fire',… |
| . Type_2 | <category> | 'Poison', 'Poison', 'Poison', 'Poison', nan, nan, 'Flying', 'Dragon',… |
| . Total | <int64> | 318, 405, 525, 625, 309, 405, 534, 634, 634, 314, 405, 530, 630, 195, 205,… |
| . HP | <int64> | 45, 60, 80, 80, 39, 58, 78, 78, 78, 44, 59, 79, 79, 45, 50, 60, 40, 45, 65,… |
| . Attack | <int64> | 49, 62, 82, 100, 52, 64, 84, 130, 104, 48, 63, 83, 103, 30, 20, 45, 35, 25,… |
| . Defense | <int64> | 49, 63, 83, 123, 43, 58, 78, 111, 78, 65, 80, 100, 120, 35, 55, 50, 30, 50,… |
| . Sp_Atk | <int64> | 65, 80, 100, 122, 60, 80, 109, 130, 159, 50, 65, 85, 135, 20, 25, 90, 20,… |
| . Sp_Def | <int64> | 65, 80, 100, 120, 50, 65, 85, 85, 115, 64, 80, 105, 115, 20, 25, 80, 20,… |
| . Speed | <int64> | 45, 60, 80, 80, 65, 80, 100, 100, 100, 43, 58, 78, 78, 45, 30, 70, 50, 35,… |
| . Generation | <category> | 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,… |
| . Legendary | <bool> | False, False, False, False, False, False, False, False, False, False,… |