Bulk rename variables from CCAO SQL to standardized or pretty names and visa versa#
- ccao.vars_rename(data: List[str] | DataFrame, names_from: str, names_to: str, output_type: str = 'inplace', dictionary: DataFrame | None = None) List[str] | DataFrame #
Rename variables from one naming convention to another.
This function renames columns in a dataset based on a dictionary that maps names from one convention to another. It can rename columns in-place or return a character vector of renamed columns, behavior that is configurable using the output_type argument.
- Parameters:
data (pandas.DataFrame or list[str]) – DataFrame or list of column names to rename. If a DataFrame, renames columns directly.
names_from (str) – The source naming convention to rename from. Must match a key in the dictionary.
names_to (str) – The target naming convention to rename to. Must match a key in the dictionary.
output_type (str) – Output type. Either
"inplace"
, which mutates the input data frame, or"vector"
, which returns a list of strings with the construction new_col_name = old_col_name.dictionary (pandas.DataFrame) – The dictionary for mapping column names. Must contain keys like
var_name_<names_from>
andvar_name_<names_to>
.
- Raises:
ValueError – If required arguments are invalid or the dictionary does not meet format requirements.
TypeError – If
data
is neither a DataFrame nor a list of column names.
- Returns:
Either the input data with renamed columns if
output_type
is"inplace"
and the input data is a DataFrame, otherwise a list of renamed columns.- Return type:
pandas.DataFrame or list[str]
- Example:
import ccao ccao.vars_rename( data=["char_yrblt"], names_from="athena", names_to="pretty", output_type="vector" )