如何将多个fi中特定列的值求和

2024-09-30 04:39:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我有多个数据文件。它们都包含4列。我想将第三列中的每个值与之前所有文件中各自的值相加(第四列也是如此)。第一列和第二列需要保持不变。最后,将最终值保存为一个单独的输出文件。 我已经写下了代码,但我不知道我应该如何进一步得到我想要的。你知道吗

```
import glob
import numpy as np
# Reading the inputs
path = r'C:\Users\hp\Desktop\test\vdfi-0**-01000000'
my_files = glob.glob(path)
#print(len(my_files))
# opening an Output file
f=open(r'C:\Users\hp\Desktop\test\vdfi.txt',"a+")
#
x = 0 
for files in my_files:
    FR=open(files,'r')
    arr=np.loadtxt(FR.name)
    Vpara=arr[:,0]; Vperp=arr[:,1];F=arr[:,2]; dF=arr[:,3]
#    F[:,i]#+=F[i+1]
    print(F)
    for i in F:
        print(i+(i+1))
#    print(F[:])
```

这只是我的输入文件外观的两个示例。你知道吗

vdfi-000-01000000
     -0.2900E+00      0.5000E-02      3.0000E+00      2.0000E+00
     -0.2900E+00      0.1000E-01      5.0000E+00      3.0000E+00
     -0.2900E+00      0.1500E-01      7.0000E+00      4.0000E+00
     -0.2900E+00      0.2000E-01      9.0000E+00      5.0000E+00
     -0.2900E+00      0.2500E-01      1.1000E+01      6.0000E+00
     -0.2900E+00      0.3000E-01      0.0000E+00      7.0000E+00
     -0.2900E+00      0.3500E-01      0.0000E+00      0.0000E+00
     -0.2900E+00      0.4000E-01      0.0000E+00      0.0000E+00
     -0.2900E+00      0.4500E-01      0.0000E+00      0.0000E+00
     -0.2900E+00      0.5000E-01      0.0000E+00      0.0000E+00
      ...             ...             ...             ...

vdfi-001-01000000
     -0.2900E+00      0.5000E-02      2.0000E+00      8.0000E+00
     -0.2900E+00      0.1000E-01      4.0000E+00      3.1000E+00
     -0.2900E+00      0.1500E-01      6.0000E+00      6.0000E+00
     -0.2900E+00      0.2000E-01      8.0000E+00      4.0000E+00
     -0.2900E+00      0.2500E-01      1.0000E+01      4.0000E+00
     -0.2900E+00      0.3000E-01      0.0000E+00      1.0000E+00
     -0.2900E+00      0.3500E-01      0.0000E+00      0.0000E+00
     -0.2900E+00      0.4000E-01      0.0000E+00      0.0000E+00
     -0.2900E+00      0.4500E-01      0.0000E+00      0.0000E+00
     -0.2900E+00      0.5000E-01      0.0000E+00      1.0000E+00
      ...             ...             ...             ...

The expected output for just these two files would be:
vdfi.txt
     -0.2900E+00      0.5000E-02      5.0000E+00      1.0000E+01
     -0.2900E+00      0.1000E-01      9.0000E+00      6.1000E+00
     -0.2900E+00      0.1500E-01      1.3000E+01      1.0000E+01
     -0.2900E+00      0.2000E-01      1.7000E+01      9.0000E+00
     -0.2900E+00      0.2500E-01      2.1000E+01      1.0000E+01
     -0.2900E+00      0.3000E-01      0.0000E+00      8.0000E+00
     -0.2900E+00      0.3500E-01      0.0000E+00      0.0000E+00
     -0.2900E+00      0.4000E-01      0.0000E+00      0.0000E+00
     -0.2900E+00      0.4500E-01      0.0000E+00      0.0000E+00
     -0.2900E+00      0.5000E-01      0.0000E+00      1.0000E+00
      ...             ...             ...             ...

现在将这两个扩展到100多个文件。因此,我只需要有一个文件包含所有以前的文件的总和,每个值基于它在其他文件上的相应值。任何建议都将不胜感激。你知道吗


Tags: 文件pathtestimportformynpfiles
1条回答
网友
1楼 · 发布于 2024-09-30 04:39:53

多亏了我的好朋友,我才能找到答案。我正在写下答案,这样对其他人也有帮助。你知道吗

import pandas as pd
import os
import numpy as np
path = "C:\\Users\\hp\\Desktop\\test\\New folder\\"
#print(path)
final_df = None
for file in os.listdir(path):
#    print(file)
    df = pd.read_csv(path+file, header = None, sep = '     ',engine = 'python')  
    #Convert to numeric
    for col in df:
        df[col] = pd.to_numeric(df[col])
    if final_df is None:
        final_df = df.copy()
    else:
        final_df[2] = final_df[2]+df[2] 
        final_df[3] = final_df[3]+df[3] 
np.savetxt('C:\\Users\\hp\\Desktop\\test\\vdf1.txt', final_df, fmt='%16.4e' , newline="\r\n")

相关问题 更多 >

    热门问题