我正在逐个读取多个.csv文件,占用一列并在数组中累积这些列,最后想要从中生成一个.csv文件。
enter code here
import numpy as np
import matplotlib.pyplot as plt
import os, os.path
for root, dirs, files in os.walk('C:\\Users\\me\\Desktop\\csvfiles'):
for f in files:
fullpath = os.path.join(root, f)
if os.path.splitext(fullpath)[1] == '.csv':
data = np.genfromtxt(fullpath, delimiter=',',names=['A', 'B', 'C', 'D'])
col_sel = np.array(data['B'])
col_sel = np.hstack([col_sel])
np.savetxt('test.csv', col_sel, delimiter=',')
不知何故,这不是以列方式向数组添加数据。如果这有效,其他一切都可能落实到位。有人可以告诉我为什么吗?
答案 0 :(得分:0)
如果要将它们垂直堆叠并将它们写入2D数组,最后列数等于CVS
个文件:
import numpy as np
import matplotlib.pyplot as plt
import os, os.path
n=0
for root, dirs, files in os.walk('/users/test'):
for f in files:
fullpath = os.path.join(root, f)
if os.path.splitext(fullpath)[1] == '.csv':
data = np.genfromtxt(fullpath, delimiter=',',names=['A', 'B', 'C', 'D'])
if(n==0):
col_sel = data['B']
else:
col_sel = np.vstack((col_sel,data['B']))
n+=1
np.savetxt('/users/test.csv', col_sel, delimiter=',')