Skip to main content
Glama

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
NameRequiredDescriptionDefault
mappingYesDictionary 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")
  • 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)

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/jonpspri/databeak'

If you have feedback or need assistance with the MCP directory API, please join our Discord server