Search code examples
pythonpandasdataframesubtraction

Subtract df1 from df2, df2 from df3 and so on from all data from folder


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.


Solution

  • 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')