CHARLS_meteorology.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import pandas as pd
  2. def sunlight(CHARLS_data):
  3. years = [2011, 2013,2015, 2018, 2020]
  4. for year in years:
  5. #读取日照数据
  6. sunlight_data_last = pd.read_excel(f"气象数据/2000-2020年按年逐年日照/【{year-1}年】逐年日照.xlsx")
  7. sunlight_data_last = sunlight_data_last[["城市", "累积日照"]]
  8. sunlight_data_last = sunlight_data_last.rename(columns={"累积日照":"last_sunlight"})
  9. sunlight_data_before_last = pd.read_excel(f"气象数据/2000-2020年按年逐年日照/【{year-2}年】逐年日照.xlsx")
  10. sunlight_data_before_last = sunlight_data_before_last[["城市", "累积日照"]]
  11. sunlight_data_before_last = sunlight_data_before_last.rename(columns={"累积日照":"before_sunlight"})
  12. #开始筛选出year的数据
  13. CHARLS_data_year = CHARLS_data[CHARLS_data['wave']==year]
  14. #合并日照
  15. table_merge = pd.merge(CHARLS_data_year, sunlight_data_last, left_on="city", right_on="城市", how='left')
  16. table_merge = pd.merge(table_merge, sunlight_data_before_last, left_on="city", right_on="城市", how='left')
  17. #更新CHARLS表
  18. CHARLS_data.loc[CHARLS_data['wave']==year, 'last_year_sunlight'] = table_merge['last_sunlight'].values
  19. CHARLS_data.loc[CHARLS_data['wave']==year, 'before_last_sunlight'] = table_merge['before_sunlight'].values
  20. CHARLS_data.to_csv("CHARLS_data_pollutants_mete.csv",index=False)
  21. print(year)
  22. def wind(CHARLS_data):
  23. years = [2011, 2013,2015, 2018, 2020]
  24. for year in years:
  25. #读取日照数据
  26. wind_data_last = pd.read_excel(f"气象数据/2000-2020年按年逐年风速/【{year-1}年】逐年风速.xlsx")
  27. wind_data_last = wind_data_last[["城市", "平均风速"]]
  28. wind_data_last = wind_data_last.rename(columns={"平均风速":"last_wind"})
  29. wind_data_before_last = pd.read_excel(f"气象数据/2000-2020年按年逐年风速/【{year-2}年】逐年风速.xlsx")
  30. wind_data_before_last = wind_data_before_last[["城市", "平均风速"]]
  31. wind_data_before_last = wind_data_before_last.rename(columns={"平均风速":"before_wind"})
  32. #开始筛选出year的数据
  33. CHARLS_data_year = CHARLS_data[CHARLS_data['wave']==year]
  34. #合并日照
  35. table_merge = pd.merge(CHARLS_data_year, wind_data_last, left_on="city", right_on="城市", how='left')
  36. table_merge = pd.merge(table_merge, wind_data_before_last, left_on="city", right_on="城市", how='left')
  37. #更新CHARLS表
  38. CHARLS_data.loc[CHARLS_data['wave']==year, 'last_year_wind'] = table_merge['last_wind'].values
  39. CHARLS_data.loc[CHARLS_data['wave']==year, 'before_last_wind'] = table_merge['before_wind'].values
  40. CHARLS_data.to_csv("CHARLS_data_pollutants_mete.csv",index=False)
  41. print(year)
  42. def rain(CHARLS_data):
  43. years = [2011, 2013,2015, 2018, 2020]
  44. for year in years:
  45. #读取日照数据
  46. rain_data_last = pd.read_excel(f"气象数据/2000-2020年按年逐年降水/【{year-1}年】逐年降水.xlsx")
  47. rain_data_last = rain_data_last[["城市", "累积降水"]]
  48. rain_data_last = rain_data_last.rename(columns={"累积降水":"last_rain"})
  49. rain_data_before_last = pd.read_excel(f"气象数据/2000-2020年按年逐年降水/【{year-2}年】逐年降水.xlsx")
  50. rain_data_before_last = rain_data_before_last[["城市", "累积降水"]]
  51. rain_data_before_last = rain_data_before_last.rename(columns={"累积降水":"before_rain"})
  52. #开始筛选出year的数据
  53. CHARLS_data_year = CHARLS_data[CHARLS_data['wave']==year]
  54. #合并日照
  55. table_merge = pd.merge(CHARLS_data_year, rain_data_last, left_on="city", right_on="城市", how='left')
  56. table_merge = pd.merge(table_merge, rain_data_before_last, left_on="city", right_on="城市", how='left')
  57. #更新CHARLS表
  58. CHARLS_data.loc[CHARLS_data['wave']==year, 'last_year_rain'] = table_merge['last_rain'].values
  59. CHARLS_data.loc[CHARLS_data['wave']==year, 'before_last_rain'] = table_merge['before_rain'].values
  60. CHARLS_data.to_csv("CHARLS_data_pollutants_mete.csv",index=False)
  61. print(year)
  62. def temperature(CHARLS_data):
  63. years = [2011, 2013,2015, 2018, 2020]
  64. for year in years:
  65. #读取日照数据
  66. temperature_data_last = pd.read_excel(f"气象数据/2000-2020年按年逐年气温/【{year-1}年】逐年气温.xlsx")
  67. temperature_data_last = temperature_data_last[["城市", "平均气温"]]
  68. temperature_data_last = temperature_data_last.rename(columns={"平均气温":"last_temperature"})
  69. temperature_data_before_last = pd.read_excel(f"气象数据/2000-2020年按年逐年气温/【{year-2}年】逐年气温.xlsx")
  70. temperature_data_before_last = temperature_data_before_last[["城市", "平均气温"]]
  71. temperature_data_before_last = temperature_data_before_last.rename(columns={"平均气温":"before_temperature"})
  72. #开始筛选出year的数据
  73. CHARLS_data_year = CHARLS_data[CHARLS_data['wave']==year]
  74. #合并日照
  75. table_merge = pd.merge(CHARLS_data_year, temperature_data_last, left_on="city", right_on="城市", how='left')
  76. table_merge = pd.merge(table_merge, temperature_data_before_last, left_on="city", right_on="城市", how='left')
  77. #更新CHARLS表
  78. CHARLS_data.loc[CHARLS_data['wave']==year, 'last_year_temperature'] = table_merge['last_temperature'].values
  79. CHARLS_data.loc[CHARLS_data['wave']==year, 'before_last_temperature'] = table_merge['before_temperature'].values
  80. CHARLS_data.to_csv("CHARLS_data_pollutants_mete.csv",index=False)
  81. print(year)
  82. def humidity(CHARLS_data):
  83. years = [2011, 2013,2015, 2018, 2020]
  84. for year in years:
  85. #读取日照数据
  86. humidity_data_last = pd.read_excel(f"气象数据/2000-2020年按年逐年湿度/【{year-1}年】逐年湿度.xlsx")
  87. humidity_data_last = humidity_data_last[["城市", "平均湿度"]]
  88. humidity_data_last = humidity_data_last.rename(columns={"平均湿度":"last_humidity"})
  89. humidity_data_before_last = pd.read_excel(f"气象数据/2000-2020年按年逐年湿度/【{year-2}年】逐年湿度.xlsx")
  90. humidity_data_before_last = humidity_data_before_last[["城市", "平均湿度"]]
  91. humidity_data_before_last = humidity_data_before_last.rename(columns={"平均湿度":"before_humidity"})
  92. #开始筛选出year的数据
  93. CHARLS_data_year = CHARLS_data[CHARLS_data['wave']==year]
  94. #合并日照
  95. table_merge = pd.merge(CHARLS_data_year, humidity_data_last, left_on="city", right_on="城市", how='left')
  96. table_merge = pd.merge(table_merge, humidity_data_before_last, left_on="city", right_on="城市", how='left')
  97. #更新CHARLS表
  98. CHARLS_data.loc[CHARLS_data['wave']==year, 'last_year_humidity'] = table_merge['last_humidity'].values
  99. CHARLS_data.loc[CHARLS_data['wave']==year, 'before_last_humidity'] = table_merge['before_humidity'].values
  100. CHARLS_data.to_csv("CHARLS_data_pollutants_mete.csv",index=False)
  101. print(year)
  102. if __name__ == "__main__":
  103. #读取CHARLS数据
  104. CHARLS_data = pd.read_csv("CHARLS_data_p_n.csv")
  105. CHARLS_data.to_csv("CHARLS_data_p_n_m.csv",index=False)
  106. CHARLS_data = pd.read_csv("CHARLS_data_p_n_m.csv")
  107. sunlight(CHARLS_data)
  108. CHARLS_data = pd.read_csv("CHARLS_data_p_n_m.csv")
  109. wind(CHARLS_data)
  110. CHARLS_data = pd.read_csv("CHARLS_data_p_n_m.csv")
  111. rain(CHARLS_data)
  112. CHARLS_data = pd.read_csv("CHARLS_data_p_n_m.csv")
  113. temperature(CHARLS_data)
  114. CHARLS_data = pd.read_csv("CHARLS_data_p_n_m.csv")
  115. humidity(CHARLS_data)