rename_columns
Change column names in dataframes using a dictionary mapping from old to new names for better data organization and clarity.
Instructions
Rename columns in the dataframe.
Returns: Dict with rename details
Examples: # Using dictionary mapping rename_columns(ctx, {"old_col1": "new_col1", "old_col2": "new_col2"})
# Rename multiple columns
rename_columns(ctx, {
"FirstName": "first_name",
"LastName": "last_name",
"EmailAddress": "email"
})
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| mapping | Yes | Dictionary mapping old column names to new names |
Implementation Reference
- The core handler function for the 'rename_columns' tool. It validates that the old column names exist in the DataFrame, renames the columns using pandas' df.rename(columns=mapping), updates the session DataFrame, and returns a RenameColumnsResult.async def rename_columns( ctx: Annotated[Context, Field(description="FastMCP context for session access")], mapping: Annotated[ dict[str, str], Field(description="Dictionary mapping old column names to new names"), ], ) -> RenameColumnsResult: """Rename columns in the dataframe. Returns: Dict with rename details Examples: # Using dictionary mapping rename_columns(ctx, {"old_col1": "new_col1", "old_col2": "new_col2"}) # Rename multiple columns rename_columns(ctx, { "FirstName": "first_name", "LastName": "last_name", "EmailAddress": "email" }) """ # Get session_id from FastMCP context session_id = ctx.session_id session, df = get_session_data(session_id) # Validate columns exist missing_cols = [col for col in mapping if col not in df.columns] if missing_cols: raise ColumnNotFoundError(missing_cols[0], df.columns.tolist()) # Apply renaming session.df = df.rename(columns=mapping) # No longer recording operations (simplified MCP architecture) return RenameColumnsResult( renamed=mapping, columns=list(mapping.values()), )
- Pydantic model defining the output schema for the rename_columns tool response, including the rename mapping and the new column names.class RenameColumnsResult(BaseToolResponse): """Result of renaming columns.""" model_config = ConfigDict(extra="forbid") renamed: dict[str, str] = Field(description="Mapping of old names to new names") columns: list[str] = Field(description="List of final column names")
- src/databeak/servers/column_server.py:654-654 (registration)Registers the rename_columns handler function as an MCP tool named 'rename_columns' on the column_server FastMCP instance.column_server.tool(name="rename_columns")(rename_columns)