rename
rename(__data, **kwargs)
Rename columns of a table.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__data |
The input table. |
required | |
**kwargs |
Keyword arguments of the form new_name = _.old_name, or new_name = "old_name". |
{} |
Examples:
>>> import pandas as pd
>>> from siuba import _, rename, select
>>> df = pd.DataFrame({"zzz": [1], "b": [2]})
>>> df >> rename(a = _.zzz)
a b
0 1 2
Note that this is equivalent to this select code:
>>> df >> select(_.a == _.zzz, _.b)
a b
0 1 2
Source code in siuba/dply/verbs.py
@singledispatch2(DataFrame)
def rename(__data, **kwargs):
"""Rename columns of a table.
Parameters
----------
__data:
The input table.
**kwargs:
Keyword arguments of the form new_name = _.old_name, or new_name = "old_name".
Examples
--------
>>> import pandas as pd
>>> from siuba import _, rename, select
>>> df = pd.DataFrame({"zzz": [1], "b": [2]})
>>> df >> rename(a = _.zzz)
a b
0 1 2
Note that this is equivalent to this select code:
>>> df >> select(_.a == _.zzz, _.b)
a b
0 1 2
"""
# TODO: allow names with spaces, etc..
col_names = {simple_varname(v):k for k,v in kwargs.items()}
if None in col_names:
raise ValueError("Rename needs column name (e.g. 'a' or _.a), but received %s"%col_names[None])
return __data.rename(columns = col_names)