How to Automate Rainfall Word Reports with Python and Pandas
This article walks through reading monthly rainfall data with pandas, cleaning missing values, calculating rainfall deviations, generating descriptive paragraphs, and rendering a formatted Word report using docxtpl, providing complete code snippets and example outputs for each step.
Overview
The author, a pandas data‑processing expert, demonstrates how to transform a left‑hand table of rainfall observations into a fully formatted Word statistical report, handling many conditional cases programmatically.
Step 1: Read Data
import pandas as pd
df = pd.read_csv("11月份数据.csv", encoding='gbk')
month = 11
df = df.query('月份==@month')
df.head(10)Step 2: Filter Missing Values
pd.isnull(df).sum()The result shows a single missing value, which is removed:
df.dropna(inplace=True)Step 3: Count Rainfall Changes
rainfall_high = df.eval('`降雨距平(mm)` > 0').value_counts().get(True, 0)
rainfall_equal = df.eval('`降雨距平(mm)` == 0').value_counts().get(True, 0)
rainfall_low = df.eval('`降雨距平(mm)` < 0').value_counts().get(True, 0)
print(rainfall_high, rainfall_equal, rainfall_low)Output:
13 1 18Step 4: Generate First Paragraph
p1 = f"{month}月份"
if rainfall_low == 0 or rainfall_high == 0:
if rainfall_equal != 0:
p1 += f"除{rainfall_equal}个观测站降雨量较往年无变化外,"
if rainfall_high == 0:
p1 += "各气象观测站降雨量较往年均偏低。"
elif rainfall_low == 0:
p1 += "各气象观测站降雨量较往年均偏高。"
else:
# 10%以内差异认为是持平
if rainfall_high > rainfall_low * 1.1:
p1 += "大部分气象观测站降雨量较往年偏高。"
elif rainfall_low > rainfall_high * 1.1:
p1 += "大部分气象观测站降雨量较往年偏低。"
else:
p1 += "各气象观测站降雨量较往年整体持平。"
p1Result:
'11月份大部分气象观测站降雨量较往年偏低。'Step 5: Region Rainfall Extremes
p2 = ""
t = df['降雨量(mm)']
p2 += f"各区域降雨量在{t.min()}~{t.max()}mm之间,其中{df.loc[t.argmax(), '区域']}区域的降雨量最大,为{t.max()}mm。"
p2Result:
'各区域降雨量在0.0~16.0mm之间,其中51a45区域的降雨量最大,为16.0mm。'Step 6: Per‑Station Statistics
p3s = []
for station, tmp in df.groupby('观测站'):
t = tmp['降雨量(mm)']
p3 = f"各区域降雨量在{t.min()}~{t.max()}mm之间,"
# Complex conditional logic to build p3 omitted for brevity
p3s.append([station, p3])
p3sStep 7: Render Word Document
from docxtpl import DocxTemplate
tpl = DocxTemplate("docxtemplate.docx")
context = {
'month': month,
'p1': p1,
'p2': p2,
'p3s': p3s,
}
tpl.render(context)
tpl.save("11月降雨量报告.docx")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.
