add_row
In [1]:
Copied!
# https://tibble.tidyverse.org/reference/add_row.html
from datar.tibble import *
from datar.base import seq
%run nb_helpers.py
nb_header(add_row)
# https://tibble.tidyverse.org/reference/add_row.html
from datar.tibble import *
from datar.base import seq
%run nb_helpers.py
nb_header(add_row)
Try this notebook on binder.
★ add_row¶
Add one or more rows of data to an existing data frame.¶
Aliases add_case
Args:¶
_data
: Data frame to append to.
*args
: and
**kwargs
: Name-value pairs to add to the data frame.
_before
: and
_after
: row index where to add the new rows.
(default to add after the last row)
Returns:¶
The dataframe with the added rows
In [2]:
Copied!
df = tibble(x=seq(1,3), y=seq(3,1))
df >> add_row(x=4, y=0)
df = tibble(x=seq(1,3), y=seq(3,1))
df >> add_row(x=4, y=0)
Out[2]:
x | y | |
---|---|---|
<int64> | <int64> | |
0 | 1 | 3 |
1 | 2 | 2 |
2 | 3 | 1 |
3 | 4 | 0 |
In [3]:
Copied!
df >> add_row(x=4, y=0, _before=2) # 0-based
df >> add_row(x=4, y=0, _before=2) # 0-based
Out[3]:
x | y | |
---|---|---|
<int64> | <int64> | |
0 | 1 | 3 |
1 | 2 | 2 |
2 | 4 | 0 |
3 | 3 | 1 |
In [4]:
Copied!
df >> add_row(x=[4,5], y=[0,-1])
df >> add_row(x=[4,5], y=[0,-1])
Out[4]:
x | y | |
---|---|---|
<int64> | <int64> | |
0 | 1 | 3 |
1 | 2 | 2 |
2 | 3 | 1 |
3 | 4 | 0 |
4 | 5 | -1 |
In [5]:
Copied!
df >> add_row(tibble_row(x = 4, y = 0))
df >> add_row(tibble_row(x = 4, y = 0))
Out[5]:
x | y | |
---|---|---|
<int64> | <int64> | |
0 | 1 | 3 |
1 | 2 | 2 |
2 | 3 | 1 |
3 | 4 | 0 |
In [6]:
Copied!
# Absent variables get missing values
df >> add_row(x = 4)
# Absent variables get missing values
df >> add_row(x = 4)
Out[6]:
x | y | |
---|---|---|
<int64> | <float64> | |
0 | 1 | 3.0 |
1 | 2 | 2.0 |
2 | 3 | 1.0 |
3 | 4 | NaN |
In [7]:
Copied!
# You can't create new variables
with try_catch():
df >> add_row(z = 10)
# You can't create new variables
with try_catch():
df >> add_row(z = 10)
[ValueError] New rows can't add columns: ['z']