如何解决使用Python将多个CSV导入HDF5
我尝试使用以下代码将特定路径中的多个CSV文件导入日期集HDF5文件:
import numpy as np
import h5py
import pandas as pd
import glob
yourpath = '/root/Desktop/mal/ex1'
all_files = glob.glob(yourpath + "/*.csv")
li = []
for filename in all_files:
df = pd.read_csv(filename,index_col=None,header=0)
li.append(df)
frame = pd.concat(li,axis=0,ignore_index=True)
hf = h5py.File('data.h5','w')
hf.create_dataset('dataset_1',data=frame)
hf.close()
但是我有一个错误:
第15行,在框架中= pd.concat(li,axis = 0,ignore_index = True)文件“ /usr/local/lib/python3.7/site-packages/pandas/core/reshape/concat.py”在concat sort = sort中,第281行,文件“ /usr/local/lib/python3.7/site-packages/pandas/core/reshape/concat.py”,在第329行,在init中引发ValueError(“串联“)ValueError:没有要串联的对象–
解决方法
尝试以这种方式连接csv文件:
PATH = r"/...." # your Path
extension = 'csv'
os.chdir(PATH)
csv_list = glob.glob('*.{}'.format(extension))
print(csv_list)
# creates new df
df = pd.DataFrame()
for csv in csv_list:
temp = pd.read_csv(csv)
df = pd.concat([df,temp],ignore_index=True)
df.drop_duplicates(keep='first',inplace=True)
# .... here comes the rest of your code
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。