Search code examples
pythonpandascsvexport-to-csv

Merge several columns from multiple csv files to one csv file


I'm just starting to learning Pandas and I'm currently tring to merge several columns from different csv files to one csv file. Here below is the original files.

Here is my code so far:

import pandas as pd
source_file = pd.read_csv("E:\\bachelor thesismaterials\\TrainData\\CSV\\Forward_2_4\\F358\\CH1.CSV")
column_sensor_1 = 'C1 in V'
column_to_save = source_file[column_sensor_1] 
target_file = pd.DataFrame(data={'':[column_to_save]},dtype = int) 
target_file.to_csv('E:\\bachelor thesis materials\\1d\\Forward_1.csv',index=False)

original file part 1

original file part 2

original file part 3

I'm trying to combine column 'C1 in V', 'C2 in V', 'C3 in V' together and the result should looks like expected result

But when I'm using DataFrame, the new input always replace existed data in the file and the format is strange, all data is stored in a single cell.

Could u guys help? Thks a lot.


Solution

  • Try this:

    import pandas as pd
    
    forward = None
    for i in range(3):
        j = i + 1
        t = pd.read_csv(f'E:\\bachelor thesismaterials\\TrainData\\CSV\\Forward_2_4\\F358\\CH{j}.CSV')[f'C{j} in V'].to_frame()
        forward = t if forward is None else forward.join(t)
    f = 'E:\\bachelor thesis materials\\1d\\Forward_1.csv'
    forward.to_csv(f, index=False)
    forward = pd.read_csv(f)
    print(forward)
    

    Output:

        C1 in V   C2 in V  C3 in V
    0  0.008496  0.006152 -0.01221
    1  0.010059  0.004199 -0.01709
    2  0.012793  0.004004 -0.02275
    3  0.014746  0.002246 -0.02803
    4  0.018262  0.002441 -0.03311
    5  0.020801  0.002441 -0.03936
    6  0.019043  0.001855 -0.04443
    7  0.018457 -0.000490 -0.04775