rename_columns
Change column names in CSV files to improve data clarity and consistency for analysis.
Instructions
Rename columns in the dataframe.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| session_id | Yes | ||
| mapping | Yes |
Implementation Reference
- Core handler function that performs the actual column renaming using pandas DataFrame.rename(), validates input columns, updates the session DataFrame, records the operation, and returns success status with updated columns.async def rename_columns( session_id: str, mapping: Dict[str, str], ctx: Context = None ) -> Dict[str, Any]: """ Rename columns in the dataframe. Args: session_id: Session identifier mapping: Dict mapping old column names to new names ctx: FastMCP context Returns: Dict with success status and renamed columns """ try: manager = get_session_manager() session = manager.get_session(session_id) if not session or session.df is None: return {"success": False, "error": "Invalid session or no data loaded"} df = session.df # Validate columns exist missing_cols = [col for col in mapping.keys() if col not in df.columns] if missing_cols: return {"success": False, "error": f"Columns not found: {missing_cols}"} session.df = df.rename(columns=mapping) session.record_operation(OperationType.RENAME, { "mapping": mapping }) return { "success": True, "renamed": mapping, "columns": session.df.columns.tolist() } except Exception as e: logger.error(f"Error renaming columns: {str(e)}") return {"success": False, "error": str(e)}
- src/csv_editor/server.py:229-237 (registration)MCP tool registration using @mcp.tool decorator. This wrapper function defines the tool interface and delegates execution to the core implementation in transformations.py.@mcp.tool async def rename_columns( session_id: str, mapping: Dict[str, str], ctx: Context = None ) -> Dict[str, Any]: """Rename columns in the dataframe.""" return await _rename_columns(session_id, mapping, ctx)