remove_row
Remove specific rows from CSV files by index to clean datasets, delete unwanted entries, or prepare data for analysis.
Instructions
Remove a specific row from the CSV file.
Args:
filename: Name of the CSV file
row_index: Zero-based index of the row to remove
Returns:
Dictionary with removal results
Input Schema
TableJSON Schema
| Name | Required | Description | Default |
|---|---|---|---|
| filename | Yes | ||
| row_index | Yes |
Implementation Reference
- csv_mcp_server/server.py:128-144 (handler)MCP tool handler for 'remove_row'. Decorated with @mcp.tool() for registration. Delegates execution to CSVManager.remove_row, handles exceptions and returns standardized response.@mcp.tool() def remove_row(filename: str, row_index: int) -> Dict[str, Any]: """ Remove a specific row from the CSV file. Args: filename: Name of the CSV file row_index: Zero-based index of the row to remove Returns: Dictionary with removal results """ try: return csv_manager.remove_row(filename, row_index) except Exception as e: return {"success": False, "error": str(e)}
- Core logic implementation of remove_row in CSVManager class. Loads CSV with pandas, validates row index, creates backup, drops the specified row, saves the file, and returns results including the removed row data.def remove_row(self, filename: str, row_index: int) -> Dict[str, Any]: """Remove a specific row from the CSV file.""" filepath = self._get_file_path(filename) if not filepath.exists(): raise FileNotFoundError(f"CSV file '{filename}' not found") # Create backup self._create_backup(filepath) try: df = pd.read_csv(filepath) if row_index >= len(df): raise IndexError(f"Row index {row_index} out of range (max: {len(df)-1})") removed_row = df.iloc[row_index].to_dict() df = df.drop(df.index[row_index]) df.to_csv(filepath, index=False) logger.info(f"Removed row from CSV file: {filepath}") return { "success": True, "filename": filename, "removed_row": removed_row, "new_total_rows": len(df) } except Exception as e: logger.error(f"Failed to remove row: {e}") raise