Skip to main content
Glama

get_value_counts

Analyze and count unique values in a CSV column. Optionally normalize, sort, or filter top N values for efficient data insights with the CSV Editor.

Instructions

Get value counts for a column.

Input Schema

TableJSON Schema
NameRequiredDescriptionDefault
ascendingNo
columnYes
normalizeNo
session_idYes
sortNo
top_nNo

Implementation Reference

  • Core handler function that executes the get_value_counts logic using pandas DataFrame.value_counts() on the specified column in the session's dataframe.
    async def get_value_counts( session_id: str, column: str, normalize: bool = False, sort: bool = True, ascending: bool = False, top_n: Optional[int] = None, ctx: Context = None ) -> Dict[str, Any]: """ Get value counts for a column. Args: session_id: Session identifier column: Column name to count values normalize: Return proportions instead of counts sort: Sort by frequency ascending: Sort order top_n: Return only top N values ctx: FastMCP context Returns: Dict with value counts """ 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 if column not in df.columns: return {"success": False, "error": f"Column '{column}' not found"} # Get value counts value_counts = df[column].value_counts( normalize=normalize, sort=sort, ascending=ascending, dropna=False ) # Apply top_n if specified if top_n: value_counts = value_counts.head(top_n) # Convert to dict counts_dict = {} for value, count in value_counts.items(): key = str(value) if not pd.isna(value) else "NaN" counts_dict[key] = float(count) if normalize else int(count) # Calculate additional statistics unique_count = df[column].nunique(dropna=False) null_count = df[column].isna().sum() session.record_operation(OperationType.ANALYZE, { "type": "value_counts", "column": column, "normalize": normalize, "top_n": top_n }) return { "success": True, "column": column, "value_counts": counts_dict, "unique_values": int(unique_count), "null_count": int(null_count), "total_count": len(df), "normalized": normalize } except Exception as e: logger.error(f"Error getting value counts: {str(e)}") return {"success": False, "error": str(e)}
  • MCP tool registration for get_value_counts, which delegates to the implementation in analytics.py. Includes the tool schema via type annotations and docstring.
    @mcp.tool async def get_value_counts( session_id: str, column: str, normalize: bool = False, sort: bool = True, ascending: bool = False, top_n: Optional[int] = None, ctx: Context = None ) -> Dict[str, Any]: """Get value counts for a column.""" return await _get_value_counts(session_id, column, normalize, sort, ascending, top_n, ctx)
  • Import statement that brings in the get_value_counts implementation from analytics.py, aliased for use in the server registration.
    from .tools.analytics import ( get_statistics as _get_statistics, get_column_statistics as _get_column_statistics, get_correlation_matrix as _get_correlation_matrix, group_by_aggregate as _group_by_aggregate, get_value_counts as _get_value_counts, detect_outliers as _detect_outliers, profile_data as _profile_data )

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