chongqing_pm.py 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import pandas as pd
  2. from glob import glob
  3. import os
  4. def pollutant_chongqing_handle():
  5. path = "pollution/result_SO2"
  6. data = pd.read_csv(path+".csv")
  7. # 找到province列等于'重庆市'的行
  8. chongqing_rows = data[data['province'] == '重庆市']
  9. # 求这些行除了'A'列和'B'列之外的其他列的平均值
  10. avg_values = chongqing_rows.iloc[:, 2:].mean()
  11. insert = pd.DataFrame([avg_values])
  12. # 增加前两行
  13. insert['province'] = '重庆市'
  14. insert['city'] = '重庆市'
  15. df = pd.concat([data,insert])
  16. df.to_csv(path+"_p.csv", index=False)
  17. def aba_chongqing_handle():
  18. path = "aba627/result/"
  19. files = glob(path+"*.csv")
  20. for file in files:
  21. data = pd.read_csv(file)
  22. # 找到province列等于'重庆市'的行
  23. chongqing_rows = data[data['province'] == '重庆市']
  24. # 求这些行除了'A'列和'B'列之外的其他列的平均值
  25. avg_values = chongqing_rows.iloc[:, 3:].mean()
  26. insert = pd.DataFrame([avg_values])
  27. # 增加前两行
  28. insert['province'] = '重庆市'
  29. insert['city'] = '重庆市'
  30. df = pd.concat([data,insert])
  31. tmp = os.path.basename(file)
  32. file_name, extension = os.path.splitext(tmp)
  33. df.to_csv(path+file_name+"_p"+extension, index=False)
  34. if __name__ == "__main__":
  35. pollutant_chongqing_handle()
  36. # aba_chongqing_handle()