简体   繁体   English

将多个 csv 文件中的几列合并到一个 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.我刚刚开始学习 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)

original file part 1原始文件第 1 部分

original file part 2原始文件第 2 部分

original file part 3原始文件第 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我正在尝试将列 '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.

相关问题 将多个 CSV 文件中的行合并到一个 CSV 文件中并保持相同的列数 - Merge rows from multiple CSV files into one CSV file and keep same number of columns 将多个CSV文件中的列合并为一个文件,并使用for循环制作多个CSV文件 - Combine columns from several CSV files into a single file and making multiple CSV file with for loop 如何将多个不同语言的 CSV 文件合并为一个 CSV 文件? - How to merge multiple CSV files with different languages into one CSV file? 合并几个txt。 多行文件到一个 csv 文件(1 行 = 1 个文档)用于主题建模 - Merge several txt. files with multiple lines to one csv file (1 line = 1 document) for Topic Modeling 将多个csv文件合并为一个 - Merge multiple csv files into one 将共享 2 列的多个 CSV 文件合并到一个唯一的数据框中 - Merge multiple CSV files that share 2 columns into one unique data frame 如何将多个 csv 文件合并到一个文件中,其中 pandas、python 上有特定列? - How to merge multiple csv files into one file with specific columns on pandas, python? 需要从多个 csv 文件中选择“第二列”并将所有“第二列”保存在一个 csv 文件中 - Need to pick 'second column' from multiple csv files and save all 'second columns' in one csv file 将多个CSV文件中的列合并为一个文件 - Combine columns from several CSV files into a single file 将具有多列的 csv 文件加载到多个 dataframe - Load csv files with multiple columns into several dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM