Skip to main content
Glama

rename_columns

Change column names in dataframes using dictionary mapping to improve data clarity and consistency for analysis.

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 main handler function for the 'rename_columns' tool. Validates that old column names exist in the DataFrame, renames 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, including the rename mapping and final column list.
    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 FastMCP column_server 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