rename
In [1]:
Copied!
# https://dplyr.tidyverse.org/reference/rename.html
%run nb_helpers.py
from datar.data import iris
from datar.all import *
nb_header(rename, rename_with)
# https://dplyr.tidyverse.org/reference/rename.html
%run nb_helpers.py
from datar.data import iris
from datar.all import *
nb_header(rename, rename_with)
Try this notebook on binder.
★ rename¶
Rename columns¶
See original API
https://dplyr.tidyverse.org/reference/rename.html
Args:¶
_data
: A data frame
**kwargs
: Columns to rename
Returns:¶
The dataframe with new names
★ rename_with¶
Rename columns with a function¶
See original API
https://dplyr.tidyverse.org/reference/rename.html
Args:¶
_data
: A data frame
_fn
: A function to apply to column names
*args
: the columns to rename and non-keyword arguments for the _fn
.
If *args
is not provided, then assuming all columns, and
no non-keyword arguments are allowed to pass to the function, use
keyword arguments instead.
**kwargs
: keyword arguments for _fn
Returns:¶
The dataframe with new names
In [2]:
Copied!
rename(iris, petal_length='Petal_Length')
rename(iris, petal_length='Petal_Length')
Out[2]:
Sepal_Length | Sepal_Width | petal_length | Petal_Width | Species | |
---|---|---|---|---|---|
<float64> | <float64> | <float64> | <float64> | <object> | |
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
... | ... | ... | ... | ... | ... |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
145 | 6.7 | 3.0 | 5.2 | 2.3 | virginica |
146 | 6.3 | 2.5 | 5.0 | 1.9 | virginica |
147 | 6.5 | 3.0 | 5.2 | 2.0 | virginica |
148 | 6.2 | 3.4 | 5.4 | 2.3 | virginica |
149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
150 rows × 5 columns
In [3]:
Copied!
rename_with(iris, str.upper)
rename_with(iris, str.upper)
Out[3]:
SEPAL_LENGTH | SEPAL_WIDTH | PETAL_LENGTH | PETAL_WIDTH | SPECIES | |
---|---|---|---|---|---|
<float64> | <float64> | <float64> | <float64> | <object> | |
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
... | ... | ... | ... | ... | ... |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
145 | 6.7 | 3.0 | 5.2 | 2.3 | virginica |
146 | 6.3 | 2.5 | 5.0 | 1.9 | virginica |
147 | 6.5 | 3.0 | 5.2 | 2.0 | virginica |
148 | 6.2 | 3.4 | 5.4 | 2.3 | virginica |
149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
150 rows × 5 columns
In [4]:
Copied!
iris >> rename_with(str.upper, starts_with("Petal"))
iris >> rename_with(str.upper, starts_with("Petal"))
Out[4]:
Sepal_Length | Sepal_Width | PETAL_LENGTH | PETAL_WIDTH | Species | |
---|---|---|---|---|---|
<float64> | <float64> | <float64> | <float64> | <object> | |
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
... | ... | ... | ... | ... | ... |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
145 | 6.7 | 3.0 | 5.2 | 2.3 | virginica |
146 | 6.3 | 2.5 | 5.0 | 1.9 | virginica |
147 | 6.5 | 3.0 | 5.2 | 2.0 | virginica |
148 | 6.2 | 3.4 | 5.4 | 2.3 | virginica |
149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
150 rows × 5 columns
In [5]:
Copied!
iris >> rename_with(lambda x: x.replace('_', '.').lower())
iris >> rename_with(lambda x: x.replace('_', '.').lower())
Out[5]:
sepal.length | sepal.width | petal.length | petal.width | species | |
---|---|---|---|---|---|
<float64> | <float64> | <float64> | <float64> | <object> | |
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
... | ... | ... | ... | ... | ... |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
145 | 6.7 | 3.0 | 5.2 | 2.3 | virginica |
146 | 6.3 | 2.5 | 5.0 | 1.9 | virginica |
147 | 6.5 | 3.0 | 5.2 | 2.0 | virginica |
148 | 6.2 | 3.4 | 5.4 | 2.3 | virginica |
149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
150 rows × 5 columns
In [6]:
Copied!
# names can be selected by indexes
iris >> rename(Sp=4)
# names can be selected by indexes
iris >> rename(Sp=4)
Out[6]:
Sepal_Length | Sepal_Width | Petal_Length | Petal_Width | Sp | |
---|---|---|---|---|---|
<float64> | <float64> | <float64> | <float64> | <object> | |
0 | 5.1 | 3.5 | 1.4 | 0.2 | setosa |
1 | 4.9 | 3.0 | 1.4 | 0.2 | setosa |
2 | 4.7 | 3.2 | 1.3 | 0.2 | setosa |
3 | 4.6 | 3.1 | 1.5 | 0.2 | setosa |
... | ... | ... | ... | ... | ... |
4 | 5.0 | 3.6 | 1.4 | 0.2 | setosa |
145 | 6.7 | 3.0 | 5.2 | 2.3 | virginica |
146 | 6.3 | 2.5 | 5.0 | 1.9 | virginica |
147 | 6.5 | 3.0 | 5.2 | 2.0 | virginica |
148 | 6.2 | 3.4 | 5.4 | 2.3 | virginica |
149 | 5.9 | 3.0 | 5.1 | 1.8 | virginica |
150 rows × 5 columns