How to concatenate multiple csv to xarray and define coordinates?

℡╲_俬逩灬. 提交于 2019-11-28 12:01:06

问题


I have multiple csv-files, with the same rows and columns and their contained data varies depending on the date. Each csv-file is affiliated with a different date, listed in its name, e.g. data.2018-06-01.csv. A minimal example of my data looks like that: I have the 2 files, data.2018-06-01.csv and data.2019-06-01.csv, that respectively contain

user_id, weight, status
001, 70, healthy
002, 90, healthy 

and

user_id, weight, status
001, 72, healthy
002, 103, obese

My Question: How can I concatenate the csv-files into a xarray and also define that the coordinates of the xarray are user_id and date?

I tried the following code

df_all = [] 
date_arr = []

for f in [`data.2018-06-01.csv`, `data.2019-06-01.csv`]:
  date = f.split('.')[1]
  df = pd.read_csv(f)
  df_all.append(df)
  date_arr.append(date)

x_arr = xr.concat([df.to_xarray() for df in df_all], coords=[date_arr, 'user_id'])

but coords=[...] leads to an error. What can I do insted? Thanks

来源:https://stackoverflow.com/questions/58358170/how-to-concatenate-multiple-csv-to-xarray-and-define-coordinates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!