我正在使用1d numpy数组,首先进行一些数学计算,然后将所有内容保存到单个csv文件中.数据集通常具有不同的长度,我不能将它们拼合在一起.这是我能想到的最好的,但必须有一个更优雅的方式.
import numpy as np
import pandas as pd
import os
array1 = np.linspace(1,20,10)
array2 = np.linspace(12,230,10)
array3 = np.linspace(7,82,20)
array4 = np.linspace(6,55,20)
output1 = np.column_stack((array1.flatten(),array2.flatten())) #saving first array set to file
np.savetxt("tempfile1.csv", output1, delimiter=',')
output2 = np.column_stack((array3.flatten(),array4.flatten())) # doing it again second array
np.savetxt("tempfile2.csv", output2, delimiter=',')
a = pd.read_csv('tempfile1.csv') # use pandas to read both files
b = pd.read_csv("tempfile2.csv")
merged = b.join(a, rsuffix='*') # merge with panda for single file
os.remove('tempfile1.csv')
os.remove("tempfile2.csv") # delete temp files
merged.to_csv('savefile.csv', index=False) …Run Code Online (Sandbox Code Playgroud)