filter_rows
Filter rows in CSV or SQLite files based on column conditions using comparison operators like equals, contains, or greater than to extract specific data subsets.
Instructions
Filter rows based on a condition.
Args:
file_path: Path to CSV or SQLite file
column: Column name to filter on
operator: Comparison operator - 'eq', 'ne', 'gt', 'gte', 'lt', 'lte', 'contains', 'startswith', 'endswith'
value: Value to compare against
limit: Maximum number of rows to return (default 100)
Returns:
Dictionary containing:
- filter_applied: Description of the filter
- original_count: Number of rows before filtering
- filtered_count: Number of rows after filtering
- rows: Filtered rows (up to limit)
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| file_path | Yes | ||
| column | Yes | ||
| operator | Yes | ||
| value | Yes | ||
| limit | No |
Implementation Reference
- src/mcp_tabular/server.py:314-378 (handler)The handler function decorated with @mcp.tool() that implements the filter_rows tool. Loads dataset, applies conditional filter using various operators, and returns summary and filtered rows (limited).def filter_rows( file_path: str, column: str, operator: str, value: str | float | int, limit: int = 100, ) -> dict[str, Any]: """ Filter rows based on a condition. Args: file_path: Path to CSV or SQLite file column: Column name to filter on operator: Comparison operator - 'eq', 'ne', 'gt', 'gte', 'lt', 'lte', 'contains', 'startswith', 'endswith' value: Value to compare against limit: Maximum number of rows to return (default 100) Returns: Dictionary containing: - filter_applied: Description of the filter - original_count: Number of rows before filtering - filtered_count: Number of rows after filtering - rows: Filtered rows (up to limit) """ df = _load_data(file_path) if column not in df.columns: raise ValueError(f"Column '{column}' not found. Available: {df.columns.tolist()}") original_count = len(df) # Apply filter based on operator if operator == "eq": mask = df[column] == value elif operator == "ne": mask = df[column] != value elif operator == "gt": mask = df[column] > float(value) elif operator == "gte": mask = df[column] >= float(value) elif operator == "lt": mask = df[column] < float(value) elif operator == "lte": mask = df[column] <= float(value) elif operator == "contains": mask = df[column].astype(str).str.contains(str(value), case=False, na=False) elif operator == "startswith": mask = df[column].astype(str).str.startswith(str(value), na=False) elif operator == "endswith": mask = df[column].astype(str).str.endswith(str(value), na=False) else: raise ValueError( f"Unknown operator: {operator}. Use: eq, ne, gt, gte, lt, lte, contains, startswith, endswith" ) filtered_df = df[mask] return { "filter_applied": f"{column} {operator} {value}", "original_count": original_count, "filtered_count": len(filtered_df), "rows": filtered_df.head(limit).to_dict(orient="records"), "truncated": len(filtered_df) > limit, }