# BasedTab


<!-- WARNING: THIS FILE WAS AUTOGENERATED! DO NOT EDIT! -->

## Usage

### Installation

For development:

``` bash
git clone https://github.com/AnswerDotAI/basedtab.git
pip install -e .
```

Install latest from the GitHub
[repository](https://github.com/AnswerDotAI/basedtab):

``` sh
$ pip install git+https://github.com/AnswerDotAI/basedtab.git
```

or from [conda](https://anaconda.org/AnswerDotAI/basedtab)

``` sh
$ conda install -c AnswerDotAI basedtab
```

or from [pypi](https://pypi.org/project/basedtab)

``` sh
$ pip install basedtab
```

## How to use

### Load data

`read_csv` and `save_csv`

``` python
from basedtab.core import *
from nbdev.config import get_config

data_path = str(get_config().config_path/'data/toy.csv')
bf = BFrame.read_csv(data_path)
bf.head(3).df
```

<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&#10;    .dataframe tbody tr th {
        vertical-align: top;
    }
&#10;    .dataframe thead th {
        text-align: right;
    }
</style>

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: right;">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">Number</th>
<th data-quarto-table-cell-role="th">City</th>
<th data-quarto-table-cell-role="th">Gender</th>
<th data-quarto-table-cell-role="th">Age</th>
<th data-quarto-table-cell-role="th">Income</th>
<th data-quarto-table-cell-role="th">Illness</th>
</tr>
</thead>
<tbody>
<tr>
<td data-quarto-table-cell-role="th">0</td>
<td>1</td>
<td>Dallas</td>
<td>Male</td>
<td>41</td>
<td>40367.0</td>
<td>No</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">1</td>
<td>2</td>
<td>Dallas</td>
<td>Male</td>
<td>54</td>
<td>45084.0</td>
<td>No</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">2</td>
<td>3</td>
<td>Dallas</td>
<td>Male</td>
<td>42</td>
<td>52483.0</td>
<td>No</td>
</tr>
</tbody>
</table>

</div>

### Groupby

``` python
from basedpl import add, tally, square
MEAN = add.reduce_first / tally
bf.groupby("City", "Income", mean=MEAN).df.astype(int)
```

<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&#10;    .dataframe tbody tr th {
        vertical-align: top;
    }
&#10;    .dataframe thead th {
        text-align: right;
    }
</style>

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: right;">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">Income</th>
</tr>
</thead>
<tbody>
<tr>
<td data-quarto-table-cell-role="th">Dallas</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>45252</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">New York City</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>96857</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">Los Angeles</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>95264</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">Mountain View</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>135078</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">Boston</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>91554</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">Washington D.C.</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>70991</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">San Diego</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>100756</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">Austin</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>90277</td>
</tr>
</tbody>
</table>

</div>

``` python
RMSE = MEAN.under(square)
bf.head(100).groupby("Gender", ["Age", "Income"], mean=MEAN, rmse=RMSE)
```

    ┌→───────────────────────────┐
    ↓"Male"⌷                     │
    ↓             Age      Income│
    │mean 44.57407407 48301.42593│
    │rmse 45.66970793 49632.10639│
    │                            │
    │"Female"⌷                   │
    │             Age      Income│
    │mean 45.30434783 39739.36957│
    │rmse 46.73468128 40738.65853│
    └~───────────────────────────┘

### Rolling

``` python
ts = BFrame(dict(price=[5,4.3,4.1,2,10,20,15,25]))
ts.rolling("price", size=2, step=2, mean=MEAN).df
```

<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle;
    }
&#10;    .dataframe tbody tr th {
        vertical-align: top;
    }
&#10;    .dataframe thead th {
        text-align: right;
    }
</style>

<table class="dataframe" data-quarto-postprocess="true" data-border="1">
<thead>
<tr style="text-align: right;">
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th"></th>
<th data-quarto-table-cell-role="th">price</th>
</tr>
</thead>
<tbody>
<tr>
<td data-quarto-table-cell-role="th">0</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>4.65</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">1</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>3.05</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">2</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>15.00</td>
</tr>
<tr>
<td data-quarto-table-cell-role="th">3</td>
<td data-quarto-table-cell-role="th">mean</td>
<td>20.00</td>
</tr>
</tbody>
</table>

</div>

### Plot

``` python
ts.plot(title="Time Series")
```

![](index_dup1_files/figure-commonmark/cell-7-output-1.svg)

``` python
bf["Income"].hist(title="Income Distribution")
```

![](index_dup1_files/figure-commonmark/cell-8-output-1.svg)
