Master Statistical Analysis in Excel & Python: Formulas and Pandas Code
This article demonstrates how to compute key statistical metrics such as sum, average, and standard deviation on Excel data using built‑in Excel formulas and a concise Python Pandas script, comparing both approaches and showing the resulting outputs.
Introduction
A fan asked how to implement statistical calculations on Excel data using Python. The author shares two practical solutions: one using Excel formulas and another using Python with Pandas.
Method 1: Excel Formulas
Enter the following formulas directly in Excel cells and fill down to calculate the required statistics.
=SUM(B2:GG2)/COUNTIF(B2:GG2,"<>0")<br/>=STDEVA(IF(B2:GG2=0,"",B2:GG2))Method 2: Python with Pandas
Using Pandas simplifies the process. The script reads the Excel file, computes total, mean (excluding zeros), and standard deviation for each row, and prints the resulting DataFrame.
import pandas as pd
df = pd.read_excel('产品周需求.xlsx', usecols='A:GG', index_col=0)
df['total'] = df.iloc[:, :188].sum(axis=1)
df['mean'] = df.iloc[:, :188].apply(lambda x: x[x != 0].mean(), axis=1)
df['std'] = df.iloc[:, :188].apply(lambda x: x[x != 0].std(), axis=1)
print(df)Conclusion
Both the Excel formulas and the Python Pandas script produce identical results, providing flexible options for performing statistical analysis on Excel datasets.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Python Crawling & Data Mining
Life's short, I code in Python. This channel shares Python web crawling, data mining, analysis, processing, visualization, automated testing, DevOps, big data, AI, cloud computing, machine learning tools, resources, news, technical articles, tutorial videos and learning materials. Join us!
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
