[{"data":1,"prerenderedAt":768},["ShallowReactive",2],{"doc:\u002Fadvanced-data-transformation-and-cleaning\u002Fcreating-pivot-tables-from-excel-data\u002Fcreate-pivot-table-from-excel-with-pandas":3,"surround:\u002Fadvanced-data-transformation-and-cleaning\u002Fcreating-pivot-tables-from-excel-data\u002Fcreate-pivot-table-from-excel-with-pandas":760},{"id":4,"title":5,"body":6,"description":753,"extension":754,"meta":755,"navigation":73,"path":756,"seo":757,"stem":758,"__hash__":759},"docs\u002Fadvanced-data-transformation-and-cleaning\u002Fcreating-pivot-tables-from-excel-data\u002Fcreate-pivot-table-from-excel-with-pandas\u002Findex.md","How to Create Pivot Table from Excel with Pandas",{"type":7,"value":8,"toc":747},"minimark",[9,13,35,40,319,323,418,422,454,483,516,531,640,644,650,695,708,730,743],[10,11,5],"h1",{"id":12},"how-to-create-pivot-table-from-excel-with-pandas",[14,15,16,17,21,22,26,27,30,31,34],"p",{},"To ",[18,19,20],"strong",{},"create a pivot table from Excel with Pandas",", load the workbook with ",[23,24,25],"code",{},"pd.read_excel()",", aggregate the dataset via ",[23,28,29],{},"pd.pivot_table()",", and export the result using ",[23,32,33],{},"to_excel()",". This programmatic workflow replaces manual UI steps, enabling deterministic, version-controlled reporting pipelines.",[36,37,39],"h3",{"id":38},"core-implementation","Core Implementation",[41,42,47],"pre",{"className":43,"code":44,"language":45,"meta":46,"style":46},"language-python shiki shiki-themes github-light github-dark","import pandas as pd\n\n# 1. Load workbook\ndf = pd.read_excel(\"source_data.xlsx\", engine=\"openpyxl\")\n\n# 2. Build pivot table\npivot = pd.pivot_table(\n df,\n values=[\"Revenue\", \"Units\"],\n index=[\"Region\", \"Sales_Rep\"],\n columns=\"Month\",\n aggfunc={\"Revenue\": \"sum\", \"Units\": \"mean\"},\n fill_value=0,\n margins=True,\n margins_name=\"Grand Total\",\n observed=False # Retains unused categorical levels (Pandas 2.2+ default is True)\n)\n\n# 3. Export\npivot.to_excel(\"report_pivot.xlsx\", sheet_name=\"Q3_Summary\")\n","python","",[23,48,49,68,75,82,113,118,124,135,141,163,183,197,228,242,255,268,282,287,292,298],{"__ignoreMap":46},[50,51,54,58,62,65],"span",{"class":52,"line":53},"line",1,[50,55,57],{"class":56},"szBVR","import",[50,59,61],{"class":60},"sVt8B"," pandas ",[50,63,64],{"class":56},"as",[50,66,67],{"class":60}," pd\n",[50,69,71],{"class":52,"line":70},2,[50,72,74],{"emptyLinePlaceholder":73},true,"\n",[50,76,78],{"class":52,"line":77},3,[50,79,81],{"class":80},"sJ8bj","# 1. Load workbook\n",[50,83,85,88,91,94,98,101,105,107,110],{"class":52,"line":84},4,[50,86,87],{"class":60},"df ",[50,89,90],{"class":56},"=",[50,92,93],{"class":60}," pd.read_excel(",[50,95,97],{"class":96},"sZZnC","\"source_data.xlsx\"",[50,99,100],{"class":60},", ",[50,102,104],{"class":103},"s4XuR","engine",[50,106,90],{"class":56},[50,108,109],{"class":96},"\"openpyxl\"",[50,111,112],{"class":60},")\n",[50,114,116],{"class":52,"line":115},5,[50,117,74],{"emptyLinePlaceholder":73},[50,119,121],{"class":52,"line":120},6,[50,122,123],{"class":80},"# 2. Build pivot table\n",[50,125,127,130,132],{"class":52,"line":126},7,[50,128,129],{"class":60},"pivot ",[50,131,90],{"class":56},[50,133,134],{"class":60}," pd.pivot_table(\n",[50,136,138],{"class":52,"line":137},8,[50,139,140],{"class":60}," df,\n",[50,142,144,147,149,152,155,157,160],{"class":52,"line":143},9,[50,145,146],{"class":103}," values",[50,148,90],{"class":56},[50,150,151],{"class":60},"[",[50,153,154],{"class":96},"\"Revenue\"",[50,156,100],{"class":60},[50,158,159],{"class":96},"\"Units\"",[50,161,162],{"class":60},"],\n",[50,164,166,169,171,173,176,178,181],{"class":52,"line":165},10,[50,167,168],{"class":103}," index",[50,170,90],{"class":56},[50,172,151],{"class":60},[50,174,175],{"class":96},"\"Region\"",[50,177,100],{"class":60},[50,179,180],{"class":96},"\"Sales_Rep\"",[50,182,162],{"class":60},[50,184,186,189,191,194],{"class":52,"line":185},11,[50,187,188],{"class":103}," columns",[50,190,90],{"class":56},[50,192,193],{"class":96},"\"Month\"",[50,195,196],{"class":60},",\n",[50,198,200,203,205,208,210,213,216,218,220,222,225],{"class":52,"line":199},12,[50,201,202],{"class":103}," aggfunc",[50,204,90],{"class":56},[50,206,207],{"class":60},"{",[50,209,154],{"class":96},[50,211,212],{"class":60},": ",[50,214,215],{"class":96},"\"sum\"",[50,217,100],{"class":60},[50,219,159],{"class":96},[50,221,212],{"class":60},[50,223,224],{"class":96},"\"mean\"",[50,226,227],{"class":60},"},\n",[50,229,231,234,236,240],{"class":52,"line":230},13,[50,232,233],{"class":103}," fill_value",[50,235,90],{"class":56},[50,237,239],{"class":238},"sj4cs","0",[50,241,196],{"class":60},[50,243,245,248,250,253],{"class":52,"line":244},14,[50,246,247],{"class":103}," margins",[50,249,90],{"class":56},[50,251,252],{"class":238},"True",[50,254,196],{"class":60},[50,256,258,261,263,266],{"class":52,"line":257},15,[50,259,260],{"class":103}," margins_name",[50,262,90],{"class":56},[50,264,265],{"class":96},"\"Grand Total\"",[50,267,196],{"class":60},[50,269,271,274,276,279],{"class":52,"line":270},16,[50,272,273],{"class":103}," observed",[50,275,90],{"class":56},[50,277,278],{"class":238},"False",[50,280,281],{"class":80}," # Retains unused categorical levels (Pandas 2.2+ default is True)\n",[50,283,285],{"class":52,"line":284},17,[50,286,112],{"class":60},[50,288,290],{"class":52,"line":289},18,[50,291,74],{"emptyLinePlaceholder":73},[50,293,295],{"class":52,"line":294},19,[50,296,297],{"class":80},"# 3. Export\n",[50,299,301,304,307,309,312,314,317],{"class":52,"line":300},20,[50,302,303],{"class":60},"pivot.to_excel(",[50,305,306],{"class":96},"\"report_pivot.xlsx\"",[50,308,100],{"class":60},[50,310,311],{"class":103},"sheet_name",[50,313,90],{"class":56},[50,315,316],{"class":96},"\"Q3_Summary\"",[50,318,112],{"class":60},[36,320,322],{"id":321},"quick-reference-parameter-mapping","Quick Reference: Parameter Mapping",[324,325,326,339],"table",{},[327,328,329],"thead",{},[330,331,332,336],"tr",{},[333,334,335],"th",{},"Excel UI Feature",[333,337,338],{},"Pandas Equivalent",[340,341,342,353,363,373,383,393,403],"tbody",{},[330,343,344,348],{},[345,346,347],"td",{},"Rows",[345,349,350],{},[23,351,352],{},"index",[330,354,355,358],{},[345,356,357],{},"Columns",[345,359,360],{},[23,361,362],{},"columns",[330,364,365,368],{},[345,366,367],{},"Values",[345,369,370],{},[23,371,372],{},"values",[330,374,375,378],{},[345,376,377],{},"Summarize by",[345,379,380],{},[23,381,382],{},"aggfunc",[330,384,385,388],{},[345,386,387],{},"Show Grand Totals",[345,389,390],{},[23,391,392],{},"margins=True",[330,394,395,398],{},[345,396,397],{},"Replace Blanks",[345,399,400],{},[23,401,402],{},"fill_value",[330,404,405,408],{},[345,406,407],{},"Filter Context",[345,409,410,413,414,417],{},[23,411,412],{},"df.query()"," \u002F ",[23,415,416],{},"df.loc[]"," (apply pre-pivot)",[36,419,421],{"id":420},"troubleshooting-fallbacks","Troubleshooting & Fallbacks",[14,423,424,429,433,434,436,437,439,440,442,443,446,447,449,450,453],{},[18,425,426],{},[23,427,428],{},"ValueError: Index contains duplicate entries, cannot reshape",[430,431,432],"em",{},"Cause:"," Missing ",[23,435,382],{}," or overlapping ",[23,438,352],{},"\u002F",[23,441,362],{}," combinations.\n",[430,444,445],{},"Fix:"," Always specify ",[23,448,382],{},". For multi-metric outputs, pass a list or dict. If duplicates indicate dirty data, clean upstream: ",[23,451,452],{},"df = df.drop_duplicates(subset=[\"Region\", \"Month\"])",".",[14,455,456,461,463,464,467,468,470,471,474,475,478,479,482],{},[18,457,458],{},[23,459,460],{},"ModuleNotFoundError: No module named 'openpyxl'",[430,462,432],{}," Pandas delegates ",[23,465,466],{},".xlsx"," I\u002FO to external engines.\n",[430,469,445],{}," Install explicitly: ",[23,472,473],{},"pip install openpyxl xlsxwriter",". For read-heavy pipelines, use ",[23,476,477],{},"engine=\"calamine\""," with ",[23,480,481],{},"fastexcel"," for 3–5x faster parsing.",[14,484,485,491,493,494,496,497,500,501,504,505,508,509,512,513,453],{},[18,486,487,490],{},[23,488,489],{},"MemoryError"," on workbooks >500MB",[430,492,432],{}," Loading entire sheets into RAM.\n",[430,495,445],{}," Restrict footprint with ",[23,498,499],{},"usecols"," in ",[23,502,503],{},"read_excel()",", or convert high-cardinality strings to categorical dtype: ",[23,506,507],{},"df[\"Region\"] = df[\"Region\"].astype(\"category\")",". For extreme scale, process in chunks with ",[23,510,511],{},"polars"," or ",[23,514,515],{},"dask",[14,517,518,521,523,524,526,527,530],{},[18,519,520],{},"Lost Excel Formatting in Output",[430,522,432],{}," Pandas writes raw values, stripping cell formats.\n",[430,525,445],{}," Use ",[23,528,529],{},"xlsxwriter"," to apply formats programmatically:",[41,532,534],{"className":43,"code":533,"language":45,"meta":46,"style":46},"with pd.ExcelWriter(\"formatted_pivot.xlsx\", engine=\"xlsxwriter\") as writer:\n pivot.to_excel(writer, sheet_name=\"Report\")\n workbook = writer.book\n worksheet = writer.sheets[\"Report\"]\n money_fmt = workbook.add_format({\"num_format\": \"$#,##0.00\"})\n worksheet.set_column(\"B:Z\", 14, money_fmt)\n",[23,535,536,564,578,588,603,624],{"__ignoreMap":46},[50,537,538,541,544,547,549,551,553,556,559,561],{"class":52,"line":53},[50,539,540],{"class":56},"with",[50,542,543],{"class":60}," pd.ExcelWriter(",[50,545,546],{"class":96},"\"formatted_pivot.xlsx\"",[50,548,100],{"class":60},[50,550,104],{"class":103},[50,552,90],{"class":56},[50,554,555],{"class":96},"\"xlsxwriter\"",[50,557,558],{"class":60},") ",[50,560,64],{"class":56},[50,562,563],{"class":60}," writer:\n",[50,565,566,569,571,573,576],{"class":52,"line":70},[50,567,568],{"class":60}," pivot.to_excel(writer, ",[50,570,311],{"class":103},[50,572,90],{"class":56},[50,574,575],{"class":96},"\"Report\"",[50,577,112],{"class":60},[50,579,580,583,585],{"class":52,"line":77},[50,581,582],{"class":60}," workbook ",[50,584,90],{"class":56},[50,586,587],{"class":60}," writer.book\n",[50,589,590,593,595,598,600],{"class":52,"line":84},[50,591,592],{"class":60}," worksheet ",[50,594,90],{"class":56},[50,596,597],{"class":60}," writer.sheets[",[50,599,575],{"class":96},[50,601,602],{"class":60},"]\n",[50,604,605,608,610,613,616,618,621],{"class":52,"line":115},[50,606,607],{"class":60}," money_fmt ",[50,609,90],{"class":56},[50,611,612],{"class":60}," workbook.add_format({",[50,614,615],{"class":96},"\"num_format\"",[50,617,212],{"class":60},[50,619,620],{"class":96},"\"$#,##0.00\"",[50,622,623],{"class":60},"})\n",[50,625,626,629,632,634,637],{"class":52,"line":120},[50,627,628],{"class":60}," worksheet.set_column(",[50,630,631],{"class":96},"\"B:Z\"",[50,633,100],{"class":60},[50,635,636],{"class":238},"14",[50,638,639],{"class":60},", money_fmt)\n",[36,641,643],{"id":642},"automation-best-practices","Automation Best Practices",[14,645,646,647,649],{},"Scripted pivots require strict schema validation. Excel exports frequently inject trailing whitespace or hidden characters that break ",[23,648,352],{}," lookups. Normalize headers before execution:",[41,651,653],{"className":43,"code":652,"language":45,"meta":46,"style":46},"df.columns = df.columns.str.strip().str.replace(r\"\\s+\", \"_\", regex=True)\n",[23,654,655],{"__ignoreMap":46},[50,656,657,660,662,665,668,671,674,677,679,681,684,686,689,691,693],{"class":52,"line":53},[50,658,659],{"class":60},"df.columns ",[50,661,90],{"class":56},[50,663,664],{"class":60}," df.columns.str.strip().str.replace(",[50,666,667],{"class":56},"r",[50,669,670],{"class":96},"\"",[50,672,673],{"class":238},"\\s",[50,675,676],{"class":56},"+",[50,678,670],{"class":96},[50,680,100],{"class":60},[50,682,683],{"class":96},"\"_\"",[50,685,100],{"class":60},[50,687,688],{"class":103},"regex",[50,690,90],{"class":56},[50,692,252],{"class":238},[50,694,112],{"class":60},[14,696,697,698,701,702,707],{},"Pandas does not preserve cell-level formulas. If downstream consumers require live Excel calculations, export the pivot as a static table and append formula ranges using ",[23,699,700],{},"openpyxl"," post-write. This aligns with standard practices for ",[703,704,706],"a",{"href":705},"\u002Fadvanced-data-transformation-and-cleaning\u002Fcreating-pivot-tables-from-excel-data\u002F","Creating Pivot Tables from Excel Data",", where deterministic outputs depend on clean input schemas.",[14,709,710,711,478,714,512,717,720,721,724,725,729],{},"When structuring broader data workflows, treat pivot generation as the final aggregation step. Raw ingestion, type coercion, and missing-value imputation must occur upstream. For complex transformations involving window functions, rolling aggregations, or cross-sheet joins, combine ",[23,712,713],{},"groupby()",[23,715,716],{},"transform()",[23,718,719],{},"merge()"," before calling ",[23,722,723],{},"pivot_table()",". These techniques fall under ",[703,726,728],{"href":727},"\u002Fadvanced-data-transformation-and-cleaning\u002F","Advanced Data Transformation and Cleaning"," and prevent silent data corruption during automated reporting cycles.",[14,731,732,733,735,736,739,740,742],{},"Wrap ",[23,734,29],{}," in a ",[23,737,738],{},"try\u002Fexcept"," block during automation to capture malformed workbooks without halting batch jobs. Log exact ",[23,741,382],{}," mismatches or missing columns to standard error for rapid debugging. This pattern ensures reporting pipelines remain resilient across varying Excel export formats and schema drift.",[744,745,746],"style",{},"html pre.shiki code .szBVR, html code.shiki .szBVR{--shiki-default:#D73A49;--shiki-dark:#F97583}html pre.shiki code .sVt8B, html code.shiki .sVt8B{--shiki-default:#24292E;--shiki-dark:#E1E4E8}html pre.shiki code .sJ8bj, html code.shiki .sJ8bj{--shiki-default:#6A737D;--shiki-dark:#6A737D}html pre.shiki code .sZZnC, html code.shiki .sZZnC{--shiki-default:#032F62;--shiki-dark:#9ECBFF}html pre.shiki code .s4XuR, html code.shiki .s4XuR{--shiki-default:#E36209;--shiki-dark:#FFAB70}html pre.shiki code .sj4cs, html code.shiki .sj4cs{--shiki-default:#005CC5;--shiki-dark:#79B8FF}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":46,"searchDepth":70,"depth":70,"links":748},[749,750,751,752],{"id":38,"depth":77,"text":39},{"id":321,"depth":77,"text":322},{"id":420,"depth":77,"text":421},{"id":642,"depth":77,"text":643},"To create a pivot table from Excel with Pandas, load the workbook with pd.read_excel(), aggregate the dataset via pd.pivot_table(), and export the result using to_excel(). This programmatic workflow replaces manual UI steps, enabling deterministic, version-controlled reporting pipelines.","md",{},"\u002Fadvanced-data-transformation-and-cleaning\u002Fcreating-pivot-tables-from-excel-data\u002Fcreate-pivot-table-from-excel-with-pandas",{"title":5,"description":753},"advanced-data-transformation-and-cleaning\u002Fcreating-pivot-tables-from-excel-data\u002Fcreate-pivot-table-from-excel-with-pandas\u002Findex","Yv9oOa_Go7_6mmS8WRwuGAOd-nMITma-rzh8CZmDhsA",[761,764],{"title":706,"path":762,"stem":763,"children":-1},"\u002Fadvanced-data-transformation-and-cleaning\u002Fcreating-pivot-tables-from-excel-data","advanced-data-transformation-and-cleaning\u002Fcreating-pivot-tables-from-excel-data\u002Findex",{"title":765,"path":766,"stem":767,"children":-1},"Handling Missing Data in Excel Reports","\u002Fadvanced-data-transformation-and-cleaning\u002Fhandling-missing-data-in-excel-reports","advanced-data-transformation-and-cleaning\u002Fhandling-missing-data-in-excel-reports\u002Findex",1777830515205]