To read dataframe, just use pandas.read.....() functions, then convert to datar tibble if needed.¶
In [13]:
Copied!
import datar.all as dr
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
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
1. Read from CSV file¶
In [10]:
Copied!
tb_csv = dr.tibble(pd.read_csv(data_dir/'air_quality_no2_long.csv'))
print(tb_csv.head())
tb_csv = dr.tibble(pd.read_csv(data_dir/'air_quality_no2_long.csv'))
print(tb_csv.head())
city country date.utc location parameter value unit <str> <str> <str> <str> <str> <float64> <str> 0 Paris FR 2019-06-21 00:00:00+00:00 FR04014 no2 20.0 µg/m³ 1 Paris FR 2019-06-20 23:00:00+00:00 FR04014 no2 21.8 µg/m³ 2 Paris FR 2019-06-20 22:00:00+00:00 FR04014 no2 26.5 µg/m³ 3 Paris FR 2019-06-20 21:00:00+00:00 FR04014 no2 24.9 µg/m³ 4 Paris FR 2019-06-20 20:00:00+00:00 FR04014 no2 21.4 µg/m³
2. Read from Excel file¶
In [11]:
Copied!
tb_excel = dr.tibble(pd.read_excel(data_dir/"emp_sheetname.xlsx", sheet_name='emp'))
print(tb_excel)
tb_excel = dr.tibble(pd.read_excel(data_dir/"emp_sheetname.xlsx", sheet_name='emp'))
print(tb_excel)
id name salary start_date dept <object> <str> <float64> <datetime64[us]> <str> 0 1 Rick 623.30 2012-01-01 IT 1 2 Dan 515.20 2013-09-23 Operations 2 3 Michelle 611.00 2014-11-15 IT 3 4 Ryan 729.00 2014-05-11 HR 4 Gary 843.25 2015-03-27 Finance 5 6 Nina 578.00 2013-05-21 IT 6 7 Simon 632.80 2013-07-30 Operations 7 8 Guru 722.50 2014-06-17 Finance
3 .Read with more options¶
In [14]:
Copied!
tb_pokemon = dr.tibble(
pd.read_csv(
filepath_or_buffer=data_dir/"pokemon.csv",
dtype={
"Type 1": "category",
"Type 2": "category",
"Generation": "category",
"Legendary": "bool"
}
)
.drop(columns=["#"])
.pipe(lambda f: f.set_axis(f.columns.str.strip().str.replace(r"\s+", "_", regex=True).str.replace(".", ""), axis=1))
.assign(Generation = pd.col('Generation').cat.as_ordered())
)
print(
tb_pokemon
>> dr.slice_head(n=5)
)
tb_pokemon = dr.tibble(
pd.read_csv(
filepath_or_buffer=data_dir/"pokemon.csv",
dtype={
"Type 1": "category",
"Type 2": "category",
"Generation": "category",
"Legendary": "bool"
}
)
.drop(columns=["#"])
.pipe(lambda f: f.set_axis(f.columns.str.strip().str.replace(r"\s+", "_", regex=True).str.replace(".", ""), axis=1))
.assign(Generation = pd.col('Generation').cat.as_ordered())
)
print(
tb_pokemon
>> dr.slice_head(n=5)
)
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
3 VenusaurMega Venusaur Grass Poison 625 80 100 123 122 120 80 1 False
4 Charmander Fire NaN 309 39 52 43 60 50 65 1 False