How to concatenate multiple csv to xarray and defi

2020-02-16 03:29发布

问题:

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