CHARLS_PM.py 3.2 KB

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