pandas 将行转换为列并保存在单独的文件中

x3naxklr  于 2023-01-24  发布在  其他
关注(0)|答案(1)|浏览(88)

我有4个文本文件在一个文件夹中,每个文本文件包含许多行的数据如下

cat a.txt
10.0000 0.0000 10.0000 0.0000           
11.0000 0.0000 11.0000 0.0000

cat b.txt
5.1065 3.8423 2.6375 3.5098
4.7873 5.9304 1.9943 4.7599

cat c.txt
3.5257 3.9505 3.8323 4.3359
3.3414 4.0014 4.0383 4.4803

cat d.txt
1.8982 2.0342 1.9963 2.1575
1.8392 2.0504 2.0623 2.2037

我想把文本文件的每一行对应到列
file001.txt

10.0000  5.1065  3.5257  1.8982
 0.0000  3.8423  3.9505  2.0342
10.0000  2.6375  3.8323  1.9963
 0.0000  3.5098  4.3359  2.1575

file002.txt

11.0000  4.7873  3.3414  1.8329
 0.0000  5.9304  4.0014  2.0504
11.0000  1.9943  4.0383  2.0623
 0.0000  4.7599  4.4803  2.2037

最后,我想将此值5.0000 6.0000 9.0000 0.0000 1.0000 1.0000添加到每一行,因此最终输出应为
file001.txt

10.0000  5.1065  3.5257  1.8982 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
 0.0000  3.8423  3.9505  2.0342 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
10.0000  2.6375  3.8323  1.9963 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
 0.0000  3.5098  4.3359  2.1575 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000

file002.txt

11.0000  4.7873  3.3414  1.8329 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
 0.0000  5.9304  4.0014  2.0504 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
11.0000  1.9943  4.0383  2.0623 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
 0.0000  4.7599  4.4803  2.2037 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000

最后,我想在每个创建的文件的顶部附加一些注解
例如,file001.txt应该是

# 
# ascertain thin
# Metamorphs
# pch
# what is that
# 5-r
# Add the thing
# liop34
# liop36
# liop45
# liop34
# M(CM)   N(M)   O(S) P(cc)   ab       cd       efgh       ijkl       mnopq    rstuv
#
10.0000  5.1065  3.5257  1.8982 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
 0.0000  3.8423  3.9505  2.0342 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
10.0000  2.6375  3.8323  1.9963 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
 0.0000  3.5098  4.3359  2.1575 5.0000 6.0000 9.0000 0.0000 1.0000 1.0000
kqlmhetl

kqlmhetl1#

files = ["a.txt", "b.txt", "c.txt", "d.txt"]

# get number of columns per file, i.e., 4 in sample data
n_each = np.loadtxt(files[0]).shape[1]

# concatanate transposed data
arrays = np.concatenate([np.loadtxt(file).T for file in files])

# rows are in column now for easier reshaping; reshape and save
n_all = arrays.shape[1]
for n in range(n_all):
    np.savetxt(f"file{str(n+1).zfill(3)}.txt",
               arrays[:, n].reshape(n_each, len(files)).T,
               fmt="%7.4f")

要将固定数组的值添加到新数组中,您可以在平铺新值n_each次后执行水平堆叠:

# other things same as above
new_values = np.tile([5, 6, 9, 0, 1, 1], (n_each, 1))
for n in range(n_all):
    np.savetxt(f"file{str(n+1).zfill(3)}.txt",
               np.hstack((arrays[:, n].reshape(n_each, len(files)).T,
                          new_values)),
               fmt="%7.4f")

要添加注解,np.savetxt的headercomments参数非常有用。我们将字符串传递给header,由于其中已经包含“#“,因此我们通过传递comments=""来抑制np.savetxt中多余的“#“:

comment = """\
# 
# ascertain thin
# Metamorphs
# pch
# what is that
# 5-r
# Add the thing
# liop34
# liop36
# liop45
# liop34
# M(CM)   N(M)   O(S) P(cc)   ab       cd       efgh       ijkl       mnopq    rstuv
#"""

# rows are in column now for easier reshaping; reshape and save
n_all = arrays.shape[1]
new_values = np.tile([5, 6, 9, 0, 1, 1], (n_each, 1))
for n in range(n_all):
    np.savetxt(f"file{str(n+1).zfill(3)}.txt",
               np.hstack((arrays[:, n].reshape(n_each, len(files)).T,
                          new_values)),
               fmt="%7.4f",
               header=comment,
               comments="")

相关问题