CHARLS_NDVI.py 1.2 KB

1234567891011121314151617181920212223242526
  1. import pandas as pd
  2. if __name__ == "__main__":
  3. years = [2011, 2013,2015, 2018, 2020]
  4. #读取CHARLS数据
  5. CHARLS_data = pd.read_csv("CHARLS_data_pollutants_mete.csv")
  6. CHARLS_data.to_csv("CHARLS_data_pollutants_p_n_m_nd.csv",index=False)
  7. CHARLS_data = pd.read_csv("CHARLS_data_pollutants_p_n_m_nd.csv")
  8. #读取NDVI数据
  9. ndvi_data = pd.read_excel(f"NDVI/【立方数据学社】地级市等级的逐年NDVI.xlsx")
  10. for year in years:
  11. #新增两列,分别为year的去年和前年的环境值
  12. # CHARLS_data[['last_year_pm2.5', "before_last_pm2.5"]]=''
  13. #开始筛选出year的数据
  14. CHARLS_data_year = CHARLS_data[CHARLS_data['wave']==year]
  15. #两个表合并
  16. table_merge = pd.merge(CHARLS_data_year, ndvi_data, left_on="city", right_on="CITY", how='left')
  17. # table_merge_last.to_csv("123.csv",index=False)
  18. #更新CHARLS表
  19. CHARLS_data.loc[CHARLS_data['wave']==year, 'last_year_ndvi'] = table_merge[str(year-1)].values
  20. CHARLS_data.loc[CHARLS_data['wave']==year, 'before_last_ndvi'] = table_merge[str(year-2)].values
  21. print(year)
  22. CHARLS_data.to_csv("CHARLS_data_pollutants_p_n_m_nd.csv",index=False)