如何将多个csv连接到xarray并定义坐标? [英] How to concatenate multiple csv to xarray and define coordinates?
问题描述
我有多个csv文件,具有相同的行和列,并且它们包含的数据根据日期而有所不同.每个csv文件都附有不同的日期,并以其名称列出,例如data.2018-06-01.csv
.我的数据的一个最小示例如下:我有两个文件data.2018-06-01.csv
和data.2019-06-01.csv
,分别包含
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
和
user_id, weight, status
001, 72, healthy
002, 103, obese
我的问题:如何将csv文件串联到一个xarray中,并定义xarray的坐标为user_id
和date
?
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
?
我尝试了以下代码
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'])
但coords=[...]
导致错误.我该怎么办?谢谢
but coords=[...]
leads to an error. What can I do insted? Thanks
推荐答案
尝试以下方法:
import glob
import pandas as pd
path=(r'ur file')
all_file = glob.glob(path + "/*.csv")
li = []
for filename in all_file:
df = pd.read_csv(filename, index_col=None, header=0)
li.append(df)
frame = pd.concat(li, axis=0, ignore_index=True)
这篇关于如何将多个csv连接到xarray并定义坐标?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!