将多个 csv 文件中的几列合并到一个 csv 文件
Merge several columns from multiple csv files to one csv file
我刚刚开始学习 Pandas,我目前正尝试将来自不同 csv 文件的几列合并到一个 csv 文件中。下面是原始文件。
到目前为止,这是我的代码:
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
我正在尝试将列 'C1 in V'、'C2 in V'、'C3 in V' 组合在一起,结果应该类似于 expected result
但是当我使用 DataFrame 时,新输入总是替换文件中的现有数据并且格式很奇怪,所有数据都存储在一个单元格中。
你们能帮忙吗?非常感谢。
试试这个:
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)
输出:
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
我刚刚开始学习 Pandas,我目前正尝试将来自不同 csv 文件的几列合并到一个 csv 文件中。下面是原始文件。
到目前为止,这是我的代码:
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
我正在尝试将列 'C1 in V'、'C2 in V'、'C3 in V' 组合在一起,结果应该类似于 expected result
但是当我使用 DataFrame 时,新输入总是替换文件中的现有数据并且格式很奇怪,所有数据都存储在一个单元格中。
你们能帮忙吗?非常感谢。
试试这个:
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)
输出:
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