CHARLS_PM.py 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import pandas as pd
  2. from glob import glob
  3. import os
  4. def pollutant_handle(path):
  5. years = [2011, 2013,2015, 2018, 2020]
  6. #读取污染物数据
  7. pollutants_data = pd.read_csv("pollution/result_O3_p.csv")
  8. for year in years:
  9. CHARLS_data = pd.read_csv(path)
  10. print(CHARLS_data.info())
  11. #开始筛选出year的数据
  12. CHARLS_data_year = CHARLS_data[CHARLS_data['wave']==year]
  13. #两个表合并
  14. table_merge = pd.merge(CHARLS_data_year, pollutants_data, on=['province', 'city'], how='left')
  15. if str(year - 1) in table_merge.columns:
  16. #更新CHARLS表
  17. CHARLS_data.loc[CHARLS_data['wave']==year, 'last_year_O3'] = table_merge[str(year-1)].values
  18. if str(year - 2) in table_merge.columns:
  19. CHARLS_data.loc[CHARLS_data['wave']==year, 'before_last_O3'] = table_merge[str(year-2)].values
  20. CHARLS_data.to_csv("CHARLS_data_pollutants.csv",index=False)
  21. print(year)
  22. def aba_handle(path_data):
  23. years = [2011, 2013,2015, 2018, 2020]
  24. for year in years:
  25. CHARLS_data = pd.read_csv(path_data)
  26. path = "aba627/result/"
  27. #读取污染物组分
  28. last_year_file_name = path+str(year-1)+"_PM25_and_species_p.csv"
  29. before_last_file_name = path+str(year-2)+"_PM25_and_species_p.csv"
  30. last_year_pollutants_data = pd.read_csv(last_year_file_name)
  31. before_last_pollutants_data = pd.read_csv(before_last_file_name)
  32. #开始筛选出year的数据
  33. CHARLS_data_year = CHARLS_data[CHARLS_data['wave']==year]
  34. #和上一年的污染物组分文件合并
  35. last_table_merge = pd.merge(CHARLS_data_year, last_year_pollutants_data, on=['province', 'city'], how='left')
  36. CHARLS_data.loc[CHARLS_data['wave']==year, 'last_year_SO4'] = last_table_merge["SO4"].values
  37. CHARLS_data.loc[CHARLS_data['wave']==year, 'last_year_NO3'] = last_table_merge["NO3"].values
  38. CHARLS_data.loc[CHARLS_data['wave']==year, 'last_year_NH4'] = last_table_merge["NH4"].values
  39. CHARLS_data.loc[CHARLS_data['wave']==year, 'last_year_OM'] = last_table_merge["OM"].values
  40. CHARLS_data.loc[CHARLS_data['wave']==year, 'last_year_BC'] = last_table_merge["BC"].values
  41. #和上上年的污染物组分文件合并
  42. before_last_table_merge = pd.merge(CHARLS_data_year, before_last_pollutants_data, on=['province', 'city'], how='left')
  43. CHARLS_data.loc[CHARLS_data['wave']==year, 'before_last_SO4'] = before_last_table_merge["SO4"].values
  44. CHARLS_data.loc[CHARLS_data['wave']==year, 'before_last_NO3'] = before_last_table_merge["NO3"].values
  45. CHARLS_data.loc[CHARLS_data['wave']==year, 'before_last_NH4'] = before_last_table_merge["NH4"].values
  46. CHARLS_data.loc[CHARLS_data['wave']==year, 'before_last_OM'] = before_last_table_merge["OM"].values
  47. CHARLS_data.loc[CHARLS_data['wave']==year, 'before_last_BC'] = before_last_table_merge["BC"].values
  48. #更新CHARLS表
  49. CHARLS_data.to_csv("CHARLS_data_pollutants.csv",index=False)
  50. print(year)
  51. if __name__ == "__main__":
  52. #读取CHARLS数据
  53. path = "CHARLS_data_pollutants.csv"
  54. # CHARLS_data = pd.read_csv("CHARLS/result_all_new.csv")
  55. # print(CHARLS_data.info())
  56. # CHARLS_data.to_csv("CHARLS_data_pollutants.csv",index=False)
  57. #处理污染物
  58. # pollutant_handle(path)
  59. #处理PM2.5组分
  60. aba_handle(path)