How to Merge Columns from Multiple CSV Files Using Python

Merge several columns from multiple csv files to one csv file

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

How to merge multiple csv files on common columns and keep the non common ones as separate columns?

Idea is create MultiIndex by DataFrame.set_index for each DataFrame, then concat with axis=1 and last remove index=False in to_csv:

cols = ['Province/State', 'Country/Region','Lat','Long','Date']

dfs = [df1, df2, df3]
df_merged = pd.concat([x.set_index(cols) for x in dfs], axis=1)
df_merged.to_csv('merged.csv', sep=',', encoding='utf-8')

Or convert MultiIndex to column and then use index=False in to_csv:

cols = ['Province/State', 'Country/Region','Lat','Long','Date']

dfs = [df1, df2, df3]
df_merged = pd.concat([x.set_index(cols) for x in dfs], axis=1).reset_index()
df_merged.to_csv('merged.csv', sep=',', encoding='utf-8', index=False)


Related Topics



Leave a reply



Submit