select_columns
Extract specific columns from a CSV file for focused data analysis or manipulation. Ideal for isolating relevant data subsets in large datasets.
Instructions
Select specific columns from the dataframe.
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| columns | Yes | ||
| session_id | Yes |
Implementation Reference
- Core handler function that implements the select_columns tool logic: validates session and columns, selects columns using pandas, updates session dataframe, records operation, handles errors.async def select_columns( session_id: str, columns: List[str], ctx: Context = None ) -> Dict[str, Any]: """ Select specific columns from the dataframe. Args: session_id: Session identifier columns: List of column names to keep ctx: FastMCP context Returns: Dict with success status and selected 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 columns if col not in df.columns] if missing_cols: return {"success": False, "error": f"Columns not found: {missing_cols}"} session.df = df[columns].copy() session.record_operation(OperationType.SELECT, { "columns": columns, "columns_before": df.columns.tolist(), "columns_after": columns }) return { "success": True, "selected_columns": columns, "columns_removed": [col for col in df.columns if col not in columns] } except Exception as e: logger.error(f"Error selecting columns: {str(e)}") return {"success": False, "error": str(e)}
- src/csv_editor/server.py:220-227 (registration)MCP tool registration (@mcp.tool decorator) for 'select_columns', which wraps and delegates to the core implementation imported as _select_columns from transformations.py.@mcp.tool async def select_columns( session_id: str, columns: List[str], ctx: Context = None ) -> Dict[str, Any]: """Select specific columns from the dataframe.""" return await _select_columns(session_id, columns, ctx)
- src/csv_editor/server.py:188-199 (helper)Import statement that brings in the select_columns implementation from transformations.py, aliased as _select_columns for use in the server wrapper.from .tools.transformations import ( filter_rows as _filter_rows, sort_data as _sort_data, select_columns as _select_columns, rename_columns as _rename_columns, add_column as _add_column, remove_columns as _remove_columns, change_column_type as _change_column_type, fill_missing_values as _fill_missing_values, remove_duplicates as _remove_duplicates, update_column as _update_column )