{"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/71959b8cb9b2459c16e14b34f28b178ccfe14735/pandas/core/col.py#L408-L433","documentation":"Raised at evaluation time inside the closure created by pandas.col (pandas/core/col.py:426) when the deferred Expression is evaluated against a DataFrame whose columns do not contain `col_name`. Because evaluation is deferred, the failure surfaces only when assign/loc/pipe actually invokes the Expression against a frame, and the message lists the DataFrame's actual columns as a hint.","triggerScenarios":"`df.assign(new=pd.col('missing'))`, `df.loc[pd.col('missing') > 5]`, or any pd.col-derived Expression evaluated against a DataFrame lacking that column. Also when a reusable Expression is applied to multiple frames of differing schema.","commonSituations":"Typos in column names, case sensitivity (\"Name\" vs \"name\"), whitespace differences, applying a pipeline built for one dataset to another with renamed columns, or stale Expressions after a refactor.","solutions":["Inspect `df.columns.tolist()` and correct the name passed to pd.col.","If the Expression is reused across frames, guard with `if col_name in df.columns` before building it, or branch on schema.","Normalize column names up front (df.rename, str.strip, str.lower) so the Expression name matches."],"exampleFix":"# before\ndf.assign(v2=pd.col('spead') * 2)  # typo\n\n# after\ndf.assign(v2=pd.col('speed') * 2)","handlingStrategy":"validation","validationCode":"import pandas as pd\n\ndef col_if_exists(df, name):\n    if name not in df.columns:\n        raise KeyError(f\"'{name}' not in {list(df.columns)}\")\n    return pd.col(name)","typeGuard":"def column_exists(df, name) -> bool:\n    return name in df.columns","tryCatchPattern":"try:\n    df = df.assign(new=pd.col(name) * 2)\nexcept ValueError as e:\n    if 'not found' in str(e):\n        import difflib\n        cols = list(df.columns)\n        match = difflib.get_close_matches(name, cols, n=1)\n        name = match[0] if match else name\n        df = df.assign(new=pd.col(name) * 2)\n    else:\n        raise","preventionTips":["Check `name in df.columns` before building a pd.col expression for reusable pipelines.","Normalize column names (strip, lower) before applying shared Expressions.","Read the hint in the error message; it lists the actual columns."],"tags":["expression","pd-col","missing-column","deferred","valueerror"],"analyzedSha":"71959b8cb9b2459c16e14b34f28b178ccfe14735","analyzedAt":"2026-08-07T01:30:20.476Z","schemaVersion":2},"datasetVersion":"2026-08-07T03:17:09.362Z"}