{"record":{"id":"d83b5ca6dba81af5","repo":"pandas-dev/pandas","slug":"column-col-name-not-found-in-given-dataframe","errorCode":null,"errorMessage":"Column '{col_name}' not found in given DataFrame.\n\nHint: did you mean one of {columns_str} instead?","messagePattern":"Column '(.+?)' not found in given DataFrame\\.\n\nHint: did you mean one of (.+?) instead\\?","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"pandas/core/col.py","lineNumber":426,"sourceCode":"          name  speed\n    1  narwhal    110\n    \"\"\"\n    if not isinstance(col_name, Hashable):\n        msg = f\"Expected Hashable, got: {type(col_name)}\"\n        raise TypeError(msg)\n\n    def func(df: DataFrame) -> Series:\n        if col_name not in df.columns:\n            columns_str = str(df.columns.tolist())\n            max_len = 90\n            if len(columns_str) > max_len:\n                columns_str = columns_str[:max_len] + \"...]\"\n\n            msg = (\n                f\"Column '{col_name}' not found in given DataFrame.\\n\\n\"\n                f\"Hint: did you mean one of {columns_str} instead?\"\n            )\n            raise ValueError(msg)\n        return df[col_name]\n\n    return Expression(func, f\"col({col_name!r})\")\n\n\n__all__ = [\"Expression\", \"col\"]\n","sourceCodeStart":408,"sourceCodeEnd":433,"githubUrl":"https://github.com/pandas-dev/pandas/blob/3b7651241d4da534b3559b60ef128e1c34f54116/pandas/core/col.py#L408-L433","documentation":"Raised inside the closure returned by pd.col() when the Expression is evaluated against a DataFrame whose columns do not contain col_name. Evaluation is deferred, so this fires at the point of use (e.g. when df.assign or df.loc materializes the expression), not at pd.col() call time. The message includes a hint listing the DataFrame's columns (truncated to 90 chars) to suggest the intended name.","triggerScenarios":"df.assign(new=pd.col('misspelled')); df.loc[pd.col('absent') > 0]; any consumer of Expression whose DataFrame lacks the column. Triggered when _eval_expression runs the inner func.","commonSituations":"Renaming/dropping columns after building an Expression; using pd.col against a DataFrame of a different schema; case/whitespace mismatches in column names.","solutions":["Confirm col_name in df.columns at the point the Expression is consumed.","Build the Expression after schema fixes (rename/strip) are applied.","Use the hint in the error to pick the correct column name."],"exampleFix":"// before\ndf = df.assign(total=pd.col('prize'))\n// after\nassert 'price' in df.columns, df.columns.tolist()\ndf = df.assign(total=pd.col('price'))","handlingStrategy":"validation","validationCode":"if col_name not in df.columns:\n    raise ValueError(f'{col_name} not in {df.columns.tolist()}')\nexpr = pd.col(col_name)\ndf.assign(new=expr)","typeGuard":"def column_exists(df, name) -> bool:\n    return name in df.columns","tryCatchPattern":null,"preventionTips":["Build pd.col Expressions only after final schema is in place.","Confirm column membership before consuming an Expression against a DataFrame.","Read the hint in the error to spot typo fixes."],"tags":["value-error","col","expression","column-not-found","deferred","schema-mismatch"],"backgroundTag":null,"analyzedSha":"3b7651241d4da534b3559b60ef128e1c34f54116","analyzedAt":"2026-08-11T22:10:44.015Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}