0

I´m trying to write a short program that will allow me to subtract values in one file (file A.dat) from other files **(B0.dat, B1.dat, B2.dat.....) I want to make a program that will repeat the process of subtracting the values A file values as many Bfiles I have(3, 7, or 81). All the files have the same amount columns. File A has one row, files B have 2rows + of co I guess the best solution would be to do a loop but I´m having errors. And at the end each corrected file I´d like to save as B0sub, B1sub, B2sub.....

file A. dat 

A   B   C   D
-1  2   2.5 4


file B0.dat         
A   B   C   D
7   8   9   10
5   3   13  11

file B1.dat 
A   B   C   D
11  12  13  14
3   4   7   8

file B2.dat 
A   B   C   D
6   8.5 5.3 1
0.8 4.2 2   9

I have totally no idea how to do it so far I tried this 


import os
filepath = 'location of files'


i = 0   
filename = f'B{i}.dat'    
file = pd.read_csv(filepath, delimiter='\t', decimal=',', header=0)
## adding 'sub' to the file 
for file in files 
    os.rename(os.path.join(directory,file), os.path.join, file + 'sub' + '.dat')
    

# next file 
 i += 1
filename = f'B{i}.dat'`````
UglyBetty
  • 17
  • 1

1 Answers1

0
  1. Please use CSV if you have comme separated data
  2. In your future questions, please provide a simple way for us to recreate the data you have (as I did in my answer)
  3. Just use B = B - A.loc[0] to substract one row to an entire dataframe

The code to create some sample data:

import pandas as pd
import glob

# Create sample data
data_A = pd.DataFrame(data={"A": [-1], "B": [2]})

data_A.to_csv("A.csv", index=False)

data_B0 = pd.DataFrame(data={"A": [7, 5], "B": [8, 3]})
data_B1 = pd.DataFrame(data={"A": [11, 3], "B": [12, 4]})
data_B0.to_csv("B0.csv", index=False)
data_B1.to_csv("B1.csv", index=False)

The code to read the data and substract A = pd.read_csv("A.csv")

for f in glob.glob("B*.csv"):
    B = pd.read_csv(f)
    B = B - A.loc[0]
    print(B)
    B.to_csv("sub_" + f, index=False)

Displays:

# For B0:
   A  B
0  8  6
1  6  1

# For B1:
    A   B
0  12  10
1   4   2
Be Chiller Too
  • 2,502
  • 2
  • 16
  • 42
  • Thank you very much for your quick answers. You gave me some ideas so I´ll keep trying :) As far as I understood your program is for two files. In reality, I have a lot of type B files with much more columns and rows that´s why I´d like to automatize it – UglyBetty Nov 10 '21 at 10:41
  • Specifically here I wrote an example for 2 files that contain 2 columns, but my answer works for a big number of B files with lots of columns. The shorter the code, the more comprehensible it is – Be Chiller Too Nov 10 '21 at 10:56