[英]Write columns of different dataframe/np.array in external files - Python
I am stacked with something that should be very easy to realize. 我堆满了一些应该非常容易实现的东西。 I have a panda data frame where each column (in total 10000) represents my x-variable. 我有一个熊猫数据框,其中每一列(总计10000)代表我的x变量。 I have also another panda data frame that is my y-variable and it is composed by one column only. 我还有另一个熊猫数据框,它是我的y变量,仅由一列组成。 I would like to create external files where I can find in file0->[y,x[0]], in file1->[y,x[1]], etc. etc. At the beginning I though to put everything together in one only file by concatenating the variables: 我想创建外部文件,可以在文件0-> [y,x [0]],文件1-> [y,x [1]]等中找到。等等。一开始我虽然将所有内容放在一起通过连接变量在一个唯一的文件中:
new=pd.concat([time['#Time'],lc], axis=1) new.to_csv('simulated_lc.csv', sep=' ',index=False)
but with 10000 columns it is not so pratical to use then the data file. 但是如果有10000列,那么使用数据文件并不是那么实际。
I also tried with another approach: instead of putting my variables inside a dataframe, I defined them as array. 我还尝试了另一种方法:我没有将变量放在数据框中,而是将它们定义为数组。 So, I have the x-variable that is x[i,j] where each i-row is the dataset that I want to write in the i-file together with the y-variable that is a one-dimensional array: 因此,我有一个x变量x [i,j],其中每个i行是我要与一个一维数组的y变量一起写入i文件的数据集:
for i in range(0,10000):
fname='lc'+str(i)+'.txt'
dataset=[x[i],y]
np.savetxt(fname,dataset)
The only problem I have is when I open the file the data are not written as two separate columns, like: 我唯一的问题是,当我打开文件时,数据没有写成两个单独的列,例如:
0 1
2 3
3 4
...
How can I solve it? 我该如何解决? Thank you. 谢谢。
How about this: 这个怎么样:
z = x.join(y, lsuffix='L', rsuffix='R')
for i in range(0,1000):
fname='lc'+str(i)+'.csv'
z.to_csv(fname, index=i)
Simply use pd.concat
across a loop of X's columns using double bracket slicer, [[...]]
: 使用双括号切片器[[...]]
只需在X的列循环中使用pd.concat
:
for col in x.columns:
fname='lc'+str(i)+'.txt'
dataset = pd.concat([y, x[[col]]], axis=1)
dataset.to_csv(fname, sep=' ', index=False)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.