I have a few data frames as CSV files in the folder.
example1_result.csv
example2_result.csv
example3_result.csv
example4_result.csv
example5_result.csv
My each data frame looks like following
TestID Result1 Result2 Result3
0 0 5 1
1 1 0 4
2 2 1 2
3 3 0 0
4 4 3 0
5 5 0 1
I want to subtract example1_result.csv from example2_result.csv on the Result1, Result2, and Result3 columns, and save it as a new data frame as result1.csv. Then the similar subtraction operation on example2_result.csv from example3_result.csv, and so on.
I want to do it using python scripts. Please help me as I am a novice in python. Thanks.
CodePudding user response:
import pandas as pd
df1 = pd.read_csv("file1.csv")
df2 = pd.read_csv("file2.csv")
dfresult = pd.DataFrame()
dfresult["Result1"] = df2["Result1"] - df1["Result1"] # do for all columns
dfresult.to_csv("result.csv")
CodePudding user response:
Given CSVs that look like:
TestID,Result1,Result2,Result3
0,0,5,1
1,1,0,4
2,2,1,2
3,3,0,0
4,4,3,0
5,5,0,1
Doing:
files = ['example1_result.csv', 'example2_result.csv',
'example3_result.csv', 'example4_result.csv',
'example5_result.csv']
dfs = []
for file in files:
# index_col matters, since we don't want to subtract TestID from eachother~
df = pd.read_csv(file, index_col='TestID')
dfs.append(df)
num_dfs = len(dfs)
for i, df in enumerate(dfs):
if i 1 == num_dfs:
break
df.sub(dfs[i 1]).to_csv(f'result{i 1}.csv')