{
  "name": "pandas reshaping methods compared: unstack, pivot, pivot_table and stack",
  "description": "Compares four pandas reshaping methods by what they take as input, whether they accept duplicate index pairs, what they return and which method reverses them. Compiled from the pandas 3.1.1 DataFrame.unstack API page and the pandas reshaping user guide, plus the duplicate-entry behaviour of unstack tested by running pandas 3.1.1.",
  "page": "https://casrai.org/guides/pandas-unstack",
  "publisher": "CASRAI",
  "license": "https://creativecommons.org/licenses/by/4.0/",
  "dateModified": "2026-11-16T21:59:27Z",
  "temporalCoverage": null,
  "spatialCoverage": null,
  "columns": [
    {
      "name": "Method",
      "unit": "",
      "description": ""
    },
    {
      "name": "Reshapes",
      "unit": "",
      "description": ""
    },
    {
      "name": "Duplicate index pairs",
      "unit": "",
      "description": ""
    },
    {
      "name": "Returns",
      "unit": "",
      "description": ""
    },
    {
      "name": "Inverse",
      "unit": "",
      "description": ""
    }
  ],
  "rows": [
    {
      "Method": "DataFrame.unstack",
      "Reshapes": "Index level (MultiIndex) to columns",
      "Duplicate index pairs": "No: raises ValueError (checked in pandas 3.1.1)",
      "Returns": "DataFrame, or Series for a single-level index",
      "Inverse": "DataFrame.stack"
    },
    {
      "Method": "DataFrame.pivot",
      "Reshapes": "Existing columns used as index, columns and values",
      "Duplicate index pairs": "No: requires unique index/columns pairs",
      "Returns": "DataFrame",
      "Inverse": "none stated on the pages read"
    },
    {
      "Method": "DataFrame.pivot_table",
      "Reshapes": "Existing columns used as index, columns and values",
      "Duplicate index pairs": "Yes: the user guide points to it for duplicates",
      "Returns": "DataFrame",
      "Inverse": "none stated on the pages read"
    },
    {
      "Method": "DataFrame.stack",
      "Reshapes": "Column labels to the index",
      "Duplicate index pairs": "Not applicable",
      "Returns": "Series or DataFrame",
      "Inverse": "DataFrame.unstack"
    }
  ],
  "sources": [
    {
      "title": "pandas DataFrame.unstack",
      "url": "https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.unstack.html",
      "accessed": "2026-11-14"
    },
    {
      "title": "pandas user guide: Reshaping and pivot tables",
      "url": "https://pandas.pydata.org/docs/user_guide/reshaping.html",
      "accessed": "2026-11-14"
    }
  ]
}