以特定方式合并大型csv文件

时间:2020-10-30 19:13:25

标签: java python bash csv optimization

我还看到了许多其他问题,但情况略有不同。

我正在尝试制作/查找一个将以以下方式合并csv文件的程序。

file1.csv 

colHeadders    1col1head    1col2head      1col3head ...

row1head       1data[1][1]  1data[1][2]  1data[1][3] ...

row2head       1data[2][1]  1data[2][2]  1data[2][3] ...

headNotShared  1data[3][1]  1data[3][2]  1data[3][3] ...

row4head       1data[4][1]  1data[4][2]  1data[4][3] ...

...


file2.csv 

colHeadders    2col1head   2col2head    2col3head ...

row1head       2data[1][1] 2data[1][2]  2data[1][3] ...

row2head       2data[2][1] 2data[2][2]  2data[2][3] ...

row4head       2data[3][1] 2data[3][2]  2data[3][3] ...

headNotShared  2data[4][1] 2data[4][2]  2data[4][3] ...

...



merged.csv 

colHeaders 1col1head   1col2head    1col3head ...    2col1head   2col2head    2col3head ...

row1head   1data[1][1] 1data[1][2]  1data[1][3] ...  2data[1][1] 2data[1][2]  2data[1][3]

row2head   1data[2][1] 1data[2][2]  1data[2][3] ...  2data[2][1] 2data[2][2]  3data[3][2]

row4head   1data[4][1]  1data[4][2]  1data[4][3] ... 2data[3][1] 2data[3][2]  2data[3][3] ...

...

另外一个棘手的部分是文件可能不会共享所有标头,在这种情况下,我希望合并的csv仅包含具有共享标头的行。如果有帮助,我正在使用的确切文件是大型Mehalation prfile,并且我正在尝试预处理数据。文件的实际外观如下:

------        03--34   08--45   08--59  ...

cg000000957   0.8950   0.8876   0.8953  ...

cg000001349   0.7687   0.7687   0.7700  ...

...


------        03--34   08--45   08--59  ...

cg000000957   0.8951   0.8877   0.8954  ...

cg000001101   0.6013   0.6100   0.6022  ...

...

知道行标题按升序排列可能会有所帮助。这些都是大文件,超过一个演出,因此速度至关重要。语言并不重要,我尝试编写bash脚本来做到这一点,但无济于事。 csv文件为txt格式,由\ space标签分隔。

2 个答案:

答案 0 :(得分:0)

我在python 3.7上运行此代码,它可以满足您的要求。

import pandas as pd
df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
                    'B': ['B0', 'B1', 'B2', 'B3'],
                    'C': ['C0', 'C1', 'C2', 'C3'],
                    'D': ['D0', 'D1', 'D2', 'D3']},
                    index=[0, 1, 2, 3])

df2 = pd.DataFrame({'B': ['B2', 'B3', 'B6', 'B7'],
                    'D': ['D2', 'D3', 'D6', 'D7'],
                    'F': ['F2', 'F3', 'F6', 'F7']},
                    index=[2, 3, 6, 7])

result = pd.concat([df1, df2], axis=1, sort=False)
print(result.dropna())

输出:

    A   B   C   D   B   D   F
2  A2  B2  C2  D2  B2  D2  F2
3  A3  B3  C3  D3  B3  D3  F3

答案 1 :(得分:0)

另一种方法(尽管Python 3.x):

# Read both file into pandas dataframe
df1 = pd.read_csv("file1.csv", sep="\t").set_index("colHeadders")
df2 = pd.read_csv("file2.csv", sep="\t").set_index("colHeadders")

# Join them with an inner join (only index contained in both dataframes are kept)
new_df = df1.join(df2, how="inner", lsuffix="_df1", rsuffix="_df2")

输出:

             1col1head_df1    1col2head_df1    1col3head_df1    2col1head_df2 ...
colHeadders                                                                    
row1head     1data[1][1]      1data[1][2]      1data[1][3]      2data[1][1] ...
row2head     1data[2][1]      1data[2][2]      1data[2][3]      2data[2][1] ...
row4head     1data[4][1]      1data[4][2]      1data[4][3]      2data[3][1] ...