In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-14 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article introduces the relevant knowledge of "how to use Python to achieve statistical analysis of rainfall". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Let's take a look at the needs:
The main thing is to automatically generate the Word statistical report on the right based on the table on the left, and the actual possibilities are far more complex than those shown in the figure.
All right, let's just start working on the code!
1 data reading
Import pandas as pddf = pd.read_csv ("November data .csv", encoding='gbk') # current statistical month month = 11df = df.query (month = @ month') df.head (10)
2 abnormal data filtering
View the number of missing values:
Pd.isnull (df) .sum ()
Results:
Regional 0 rainfall (mm) 0 rainfall anomaly (mm) 1 Observatory 0dtype: int64
Only one missing value data can be deleted directly:
Df.dropna (inplace=True)
(3) calculate the change of rainfall at the observation station compared with previous years.
It is calculated that the rainfall is higher than in previous years, unchanged from that in previous years, and lower than in previous years.
Rainfall_high = df.eval ('`rainfall anomaly (mm) `> 0'). Value_counts (). Get (True, 0) rainfall_equal = df.eval (' `rainfall anomaly (mm)` = 0'). Value_counts (). Get (True, 0) rainfall_low = df.eval ('`rainfall anomaly (mm) `
< 0').value_counts().get(True, 0)print(rainfall_high, rainfall_equal, rainfall_low)13 1 18 上面的结果中rainfall_high表示降雨量比往年平均水平高的次数,rainfall_equal表示降雨量比往年平均水平持平的次数,rainfall_low表示降雨量比往年平均水平低的次数。 于是分情况讨论生成第一段的报告: p1 = f"{month}月份"if rainfall_low == 0 or rainfall_high == 0: if rainfall_equal != 0: p1 += f"除{rainfall_equal}个观测站降雨量较往年无变化外," if rainfall_high == 0: p1 += f"各气象观测站降雨量较往年均偏低。" elif rainfall_low == 0: p1 += f"各气象观测站降雨量较往年均偏高。"else: # 10%以内差异认为是持平 if rainfall_high >Rainfall_low*1.1: P1 + = f "rainfall at most meteorological stations is higher than in previous years." Elif rainfall_low > rainfall_high*1.1: P1 + = f "rainfall at most meteorological stations is lower than that in previous years." Else: P1 + = f "the rainfall of each meteorological station is the same as that of previous years as a whole." P1
Results:
The rainfall at most meteorological stations in November is lower than in previous years.'
4 calculate the extreme value of rainfall in each region
Then generate the report of the second paragraph:
P2 = "t = df ['rainfall (mm)'] p2 + = f" the rainfall in each region ranges from {t.min ()} to {t.max ()} mm, in which {df.loc [t.argmax (), 'region']} has the largest rainfall, which is {t.max ()} mm. " P2
Results:
The rainfall in each region is between 0.0~16.0mm, and the rainfall in 51a45 region is the highest, which is 16.0mm.
5-point observation station statistics
The pain in my head starts with the code here, and there are more complex requirements that won't be released.
For each observation station, count which areas are high, which areas are flat, and which areas are low:
P3s = [] for station, tmp in df.groupby ('observatory'): t = tmp ['rainfall (mm)'] p3 = f "rainfall in each region ranges from {t.min ()} to {t.max ()} mm. "rainfall_high_mask = tmp.eval ('`rainfall anomaly (mm) `> 0') rainfall_equal_mask = tmp.eval (' `rainfall anomaly (mm)` = = 0') rainfall_low_mask = tmp.eval ('`rainfall anomaly (mm) `)
< 0') rainfall_high = rainfall_high_mask.value_counts().get(True, 0) rainfall_equal = rainfall_equal_mask.value_counts().get(True, 0) rainfall_low = rainfall_low_mask.value_counts().get(True, 0)# print(rainfall_high, rainfall_equal, rainfall_low) if rainfall_low == 0 or rainfall_high == 0: if rainfall_equal != 0: p3 += '除' p3 += '、'.join(tmp.loc[rainfall_equal_mask, '区域']+'区域') p3 += "降雨量较往年无变化外," if rainfall_high == 0: p3 += f"各区域降雨量均较往年偏低" elif rainfall_low == 0: p3 += f"各区域降雨量均较往年偏高" t = tmp['降雨距平(mm)'].abs() p3 += f"{t.min()}~{t.max()}mm;" else: if rainfall_equal != 0: p3 += '除' p3 += '、'.join(tmp.loc[rainfall_equal_mask, '区域']+'区域') p3 += "降雨量较往年无变化," # 10%以内差异认为是持平 if rainfall_high >Rainfall_low*1.1: if rainfall_equal = = 0: p3 + = 'except' p3 + =', '.join (tmp.loco [rainfall _ low_mask,' area'] + 'area') p3 + = "rainfall is lower than in previous years" t = tmp.loco [rainfall _ low_mask 'rainfall anomaly (mm)'] .abs () if t.shape [0] > 1: P3 + = f "{t.min ()} ~ {t.max ()} mm" else: p3 + = f "{t.min ()} mm" p3 + = "outside "t = tmp.loco [rainfall _ high_mask, 'rainfall anomaly (mm)'] .abs () p3 + = f" rainfall in other regions is higher {t.min ()} ~ {t.max ()} mm than in previous years. "elif rainfall_low > rainfall_high*1.1: if rainfall_equal = = 0: p3 + = 'except for' p3 + =', '.join (tmp.loco [rainfall _ high_mask,' region'] + 'region') p3 + =" rainfall is higher than in previous years "t = tmp.loco [rainfall _ high_mask 'rainfall anomaly (mm)'] .abs () if t.shape [0] > 1: P3 + = f "{t.min ()} ~ {t.max ()} mm" else: p3 + = f "{t.min ()} mm" p3 + = "outside "t = tmp.loco [rainfall _ low_mask, 'rainfall anomaly (mm)'] .abs () p3 + = f" rainfall in other regions is lower than in previous years {t.min ()} ~ {t.max ()} mm "else: if rainfall_equal! = 0: P3 = p3 [:-1] + 'outside,' p3 + = f" the amount of rainfall in each region is the same as that in previous years. Where "p3 + =', '.join (tmp.loco [rainfall _ low_mask,' region'] + 'zone') p3 + =" rainfall is lower than in previous years "t = tmp.loco [rainfall _ low_mask 'rainfall anomaly (mm)'] .abs () if t.shape [0] > 1: P3 + = f "{t.min ()} ~ {t.max ()} mm "else: P3 + = f" {t.min ()} mm "p3 + =', '.join (tmp.loco [rainfall _ high_mask,' region'] + 'zone') p3 + =" rainfall is higher than in previous years "t = tmp.loco [rainfall _ high_mask 'rainfall anomaly (mm)'] .abs () if t.shape [0] > 1: P3 + = f "{t.min ()} ~ {t.max ()} mm "else: P3 + = f" {t.min ()} mm; "p3s.append ([station, p3]) p3s [- 1] [- 1] = p3s [- 1] [- 1] [:-1] +". " P3s
6 write the organized text to word
The contents of Word template file docxtemplate.docx:
1. Actual rainfall of various meteorological stations in {{month}} month (1) precipitation {{p2}} {% p for station,p3 in p3s%} {{station}: {{p3}} {% p endfor%}
That is:
Python rendering code:
From docxtpl import DocxTemplatetpl = DocxTemplate ("docxtemplate.docx") context = {'month': month,' p1statistics: p1, 'p2statistics: p2,' p3slots: p3s,} tpl.render (context) tpl.save ("November rainfall report .docx") "how to use Python to achieve statistical analysis of rainfall" is here. Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.