mutate, transmute
mutate(__data, *args, **kwargs)
Assign new variables to a DataFrame, while keeping existing ones.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__data |
pd.DataFrame |
required | |
**kwargs |
new_col_name=value pairs, where value can be a function taking a singledispatch2 argument for the data being operated on. |
{} |
Examples:
>>> from siuba import _, mutate, head
>>> from siuba.data import cars
>>> cars >> mutate(cyl2 = _.cyl * 2, cyl4 = _.cyl2 * 2) >> head(2)
cyl mpg hp cyl2 cyl4
0 6 21.0 110 12 24
1 6 21.0 110 12 24
Source code in siuba/dply/verbs.py
@singledispatch2(pd.DataFrame)
def mutate(__data, *args, **kwargs):
"""Assign new variables to a DataFrame, while keeping existing ones.
Parameters
----------
__data: pd.DataFrame
**kwargs:
new_col_name=value pairs, where value can be a function taking a singledispatch2
argument for the data being operated on.
See Also
--------
transmute : Returns a DataFrame with only the newly created columns.
Examples
--------
>>> from siuba import _, mutate, head
>>> from siuba.data import cars
>>> cars >> mutate(cyl2 = _.cyl * 2, cyl4 = _.cyl2 * 2) >> head(2)
cyl mpg hp cyl2 cyl4
0 6 21.0 110 12 24
1 6 21.0 110 12 24
"""
new_names, df_res = _mutate_cols(__data, args, kwargs)
return df_res
transmute(__data, *args, **kwargs)
Assign new columns to a DataFrame, while dropping previous columns.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
__data |
The input data. |
required | |
**kwargs |
Each keyword argument is the name of a new column, and an expression. |
{} |
Examples:
>>> from siuba import _, transmute, mutate, head
>>> from siuba.data import cars
Notice that transmute results in a table with only the new column:
>>> cars >> transmute(cyl2 = _.cyl + 1) >> head(2)
cyl2
0 7
1 7
By contrast, mutate adds the new column to the end of the table:
>>> cars >> mutate(cyl2 = _.cyl + 1) >> head(2)
cyl mpg hp cyl2
0 6 21.0 110 7
1 6 21.0 110 7
Source code in siuba/dply/verbs.py
@singledispatch2(DataFrame)
def transmute(__data, *args, **kwargs):
"""Assign new columns to a DataFrame, while dropping previous columns.
Parameters
----------
__data:
The input data.
**kwargs:
Each keyword argument is the name of a new column, and an expression.
See Also
--------
mutate : Assign new columns, or modify existing ones.
Examples
--------
>>> from siuba import _, transmute, mutate, head
>>> from siuba.data import cars
Notice that transmute results in a table with only the new column:
>>> cars >> transmute(cyl2 = _.cyl + 1) >> head(2)
cyl2
0 7
1 7
By contrast, mutate adds the new column to the end of the table:
>>> cars >> mutate(cyl2 = _.cyl + 1) >> head(2)
cyl mpg hp cyl2
0 6 21.0 110 7
1 6 21.0 110 7
"""
arg_vars = list(map(simple_varname, args))
col_names, df_res = _mutate_cols(__data, args, kwargs)
return df_res[col_names]