[英]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.我刚刚开始学习 Pandas 并且我目前正在尝试将来自不同 csv 文件的几列合并到一个 csv 文件中。 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)
I'm trying to combine column 'C1 in V', 'C2 in V', 'C3 in V' together and the result should looks like expected result我正在尝试将列 'C1 in V'、'C2 in V'、'C3 in V' 组合在一起,结果应该看起来像预期的结果
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.但是当我使用 DataFrame 时,新的输入总是替换文件中的现有数据并且格式很奇怪,所有数据都存储在一个单元格中。
Could u guys help?你们能帮忙吗? Thks a lot.
非常感谢。
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: 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.