Skip to main content
Glama

fill_missing_values

Fill or remove missing values in CSV files using specified strategies such as dropping rows or filling with custom values. Ideal for cleaning and preparing datasets efficiently.

Instructions

Fill or remove missing values.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
columnsNo
session_idYes
strategyNodrop
valueNo

Implementation Reference

  • Core implementation of the fill_missing_values tool. Handles various strategies (drop, fill, forward, backward, mean, median, mode) to manage NaN values in pandas DataFrame using session manager.
    async def fill_missing_values( session_id: str, strategy: str = "drop", value: Any = None, columns: Optional[List[str]] = None, ctx: Context = None ) -> Dict[str, Any]: """ Fill or remove missing values. Args: session_id: Session identifier strategy: One of 'drop', 'fill', 'forward', 'backward', 'mean', 'median', 'mode' value: Value to fill with (for 'fill' strategy) columns: Specific columns to apply to (None for all) ctx: FastMCP context Returns: Dict with success status and fill info """ 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 null_counts_before = df.isnull().sum().to_dict() if columns: 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}"} target_cols = columns else: target_cols = df.columns.tolist() if strategy == "drop": session.df = df.dropna(subset=target_cols) elif strategy == "fill": if value is None: return {"success": False, "error": "Value required for 'fill' strategy"} session.df[target_cols] = df[target_cols].fillna(value) elif strategy == "forward": session.df[target_cols] = df[target_cols].fillna(method='ffill') elif strategy == "backward": session.df[target_cols] = df[target_cols].fillna(method='bfill') elif strategy == "mean": for col in target_cols: if df[col].dtype in ['int64', 'float64']: session.df[col] = df[col].fillna(df[col].mean()) elif strategy == "median": for col in target_cols: if df[col].dtype in ['int64', 'float64']: session.df[col] = df[col].fillna(df[col].median()) elif strategy == "mode": for col in target_cols: mode_val = df[col].mode() if len(mode_val) > 0: session.df[col] = df[col].fillna(mode_val[0]) else: return {"success": False, "error": f"Unknown strategy: {strategy}"} null_counts_after = session.df.isnull().sum().to_dict() session.record_operation(OperationType.FILL_MISSING, { "strategy": strategy, "value": str(value) if value is not None else None, "columns": target_cols }) return { "success": True, "strategy": strategy, "rows_before": len(df), "rows_after": len(session.df), "null_counts_before": null_counts_before, "null_counts_after": null_counts_after } except Exception as e: logger.error(f"Error filling missing values: {str(e)}") return {"success": False, "error": str(e)}
  • MCP tool registration using @mcp.tool decorator. Thin wrapper that delegates to the core implementation in transformations.py.
    async def fill_missing_values( session_id: str, strategy: str = "drop", value: Any = None, columns: Optional[List[str]] = None, ctx: Context = None ) -> Dict[str, Any]: """Fill or remove missing values.""" return await _fill_missing_values(session_id, strategy, value, columns, ctx)
  • Input schema defined by function parameters: session_id (str), strategy (str, default 'drop'), value (Any), columns (Optional[List[str]]), ctx (Context). Output: Dict[str, Any] with success status and metrics.
    session_id: str, strategy: str = "drop", value: Any = None, columns: Optional[List[str]] = None, ctx: Context = None ) -> Dict[str, Any]: """Fill or remove missing values.""" return await _fill_missing_values(session_id, strategy, value, columns, ctx)

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/santoshray02/csv-editor'

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