Ravindra BagaleCourses & study guides मराठी Track your progress

Guides

Excel vs Python pandas: Same Tasks Side by Side, and When to Use Each

Amir learned Excel first and now his manager wants Python. He takes the same Raja Coffee Roasters orders and asks the same question both ways. Excel: =SUMIFS(Orders[Amount],Orders[City],"Pune"). pandas: df[df["City"] == "Pune"]["Amount"].sum(). Both say ₹10,700. Most everyday analysis has a direct pair like this, so the real question is not which tool is better, but which one fits the job.

pandas is a Python library for working with tables (DataFrames). Each common Excel task has a pandas pair: filters are boolean masks, SUMIFS/PivotTables are groupby and pivot_table, XLOOKUP is merge, sorting is sort_values, Remove Duplicates is drop_duplicates. Use Excel for data that fits in a sheet and for interactive, visual work; use pandas for large files, repeatable scripted pipelines and version-controlled analysis. Python in Excel lets you run pandas inside a workbook on supported Microsoft 365 plans.

Download the practice file (vs-python-pandas.xlsx)

Excel file, 11 KB. It has the sample data used below. All numbers in this workbook are fictional teaching data, not real company figures.

Quick answer

  1. Read the file: df = pd.read_excel("orders.xlsx", sheet_name="Orders") (needs the openpyxl package for .xlsx).
  2. Filter rows with a boolean mask: df[df["City"] == "Pune"] is the same as a filter on City.
  3. Summarise with df.groupby("City")["Amount"].sum(), the pandas SUMIFS or PivotTable.
  4. Look up with df.merge(customers, on="Customer", how="left"), the pandas XLOOKUP.
  5. Cross-tab with df.pivot_table(index="City", columns="Product", values="Amount", aggfunc="sum").
  6. Write back with df.to_excel("out.xlsx", index=False).

Task-by-task mapping

TaskExcelpandas
Open dataFile > Open, Get Datapd.read_excel("f.xlsx", sheet_name="Orders"), pd.read_csv("f.csv")
Look at the top rowsScrolldf.head()
FilterFilter button, FILTERdf[df["City"] == "Pune"]
Two conditions(c1)*(c2) in FILTER, SUMIFS with two pairsdf[(df["City"] == "Pune") & (df["Amount"] >= 1000)]
SortSort, SORTdf.sort_values("Amount", ascending=False)
Remove duplicatesRemove Duplicates, UNIQUEdf.drop_duplicates()
New columnFormula filled downdf["Band"] = np.where(df["Amount"] >= 1500, "Large", "Small")
Sum by groupSUMIFS, PivotTabledf.groupby("City")["Amount"].sum()
Cross-tabPivotTable with Rows and Columnsdf.pivot_table(index=..., columns=..., values=..., aggfunc="sum")
Look up from another tableXLOOKUP, Power Query Mergedf.merge(cust, on="Customer", how="left")
Stack two tablesVSTACK, Power Query Appendpd.concat([a, b])
BlanksBlank cells, ISBLANKNaN, df.isna(), df.fillna(0)
SaveSave Asdf.to_excel("out.xlsx", index=False)

The pandas documentation has its own page on this, *Comparison with spreadsheets*, which maps Excel features to pandas: pandas.pydata.org: Comparison with spreadsheets.

Side by side on the same data

We ran every pandas line below on the practice file's Orders table and checked that Excel returns the same numbers.

TaskpandasExcelAnswer
Total sales in Punedf[df["City"] == "Pune"]["Amount"].sum()=SUMIFS(Orders[Amount],Orders[City],"Pune")₹10,700
Different customersdf["Customer"].nunique()=COUNTA(UNIQUE(Orders[Customer]))8
Largest orderdf["Amount"].max()=MAX(Orders[Amount])₹3,200
Average quantitydf["Qty"].mean()=AVERAGE(Orders[Qty])2.71
Rows in the tablelen(df)=ROWS(Orders[Order ID])24
# groupby = SUMIFS / COUNTIFS / AVERAGEIFS by city
df.groupby("City")["Amount"].agg(["sum", "count", "mean"])

# merge = XLOOKUP (how='left' keeps every order)
df.merge(customers[["Customer", "Segment"]], on="Customer", how="left")

# pivot_table = PivotTable / SUMIFS grid
df.pivot_table(index="City", columns="Product", values="Amount", aggfunc="sum", fill_value=0)
Citysumcountmean
Kolhapur₹3,9003₹1,300
Nagpur₹6,4404₹1,610
Nashik₹4,7004₹1,175
Pune₹10,7008₹1,338
Solapur₹5,8005₹1,160

After a left merge, Ruhi's orders have no segment because she is not in the Customers table. pandas shows NaN; the Excel XLOOKUP shows "(no match)" because we set that as its if_not_found value.

Where each tool is stronger

PointExcelpandas
SizeUp to 10,48,576 rows by 16,384 columns per worksheet (Microsoft's published limit)Limited by your computer's memory; the pandas docs have a guide on scaling to large datasets
Seeing the dataYou see every cell and change it directlyYou see data when you print or display it
Repeating the workRe-do steps, or record them in Power Query or a macroRe-run the script on new files; every step is written down
Checking changesHard to compare two versions of a workbookScripts are plain text and work well with version control such as Git
Charts and sharingBuilt-in charts, easy to send to anyone with ExcelCharts with libraries such as Matplotlib; readers need the output, not Python
Learning curveMost office users already know itYou need to learn Python first

Rule of thumb: if the file opens comfortably and the task is one-off, stay in Excel. If the data is bigger than a sheet, arrives every week in the same shape, or needs joins across many files, script it in pandas.

Python in Excel: both in one workbook

Python in Excel lets you type =PY( in a cell and run Python, including pandas, on data from the workbook; the code runs in Microsoft's cloud. According to Microsoft's *Python in Excel availability* page, it is available to Enterprise and Business users on Windows (Current Channel from Version 2408), on Excel for the web, and on Excel for Mac (Current Channel from Version 16.96), with preview access for Family and Personal users. It needs a paid Microsoft 365 licence with desktop apps. Check the page for your plan before you build on it: Python in Excel availability.

Ravindra Bagale's Tip

Learn pandas by repeating a job you already do in Excel. Take one weekly report, write each step as a pandas line next to the Excel step, and check that every total matches. When they match for three weeks in a row, you can trust the script.

Practice task

  1. Write the pandas line for =COUNTIFS(Orders[City],"Nashik") and check it against the sheet.
  2. Write the Excel formula for df["Qty"].sum().
  3. On the pandas vs Excel sheet, add a row for average order value by Product with AVERAGEIFS, then write the groupby that matches.
  4. If you have Python, run pd.read_excel on the practice file and repeat the groupby. Do your numbers match the sheet?
  5. Which of your weekly Excel tasks would you move to pandas first, and why?

Frequently asked questions

Should analysts learn pandas or Excel first?

Excel first for most office roles: it is what colleagues use and it teaches the ideas (filters, grouping, lookups). Add pandas when files get large or reports repeat every week.

Can pandas read my Excel files?

Yes. pandas.read_excel reads .xlsx files (it uses an engine such as openpyxl) and DataFrame.to_excel writes them.

What is the pandas version of a PivotTable?

DataFrame.pivot_table, or groupby followed by unstack. Both are in the official pandas documentation.

Do I need Python installed to use Python in Excel?

No. Python in Excel runs in Microsoft's cloud, but you need a supported Microsoft 365 plan and version.