簡體   English   中英

合並具有相同列標題 PYTHON 的多個 csv

[英]Merge multiple csv with same column headers PYTHON

我可以使用 Z23EEEB4347BDD26BFC6B7EE9AB 將具有相同 header 的多個 csv 文件合並到一個 csv 中嗎? 我想保留相同的列標題,但如果我使用終端合並我的文件,它會創建一個帶有重復標題的文件; 所以有辦法在 Python 中做到這一點?

我希望這個對你有用

import pandas as pd

# this two dataframes are just for showcasing the merge
# But can be replaced but any other valid DataFrame
# even more than 2 dataframes, just need to repeat this process

df1 = pd.DataFrame()
df1['col1'] = 1,2,3
# it even supports dataframes with not exactly the same columns
df1['col3'] = 4,4,8

df2 = pd.DataFrame()
df2['col1'] = 4,5,6
df2['col2'] = 1,4,8

# here df3 gets the resulting merged DataFrame
df3 = df1.append(df2)

# As you can see they get merged
# but as the df1 didnt had the "col2" its values are NaN
# Same with df2 and "col3"
>>   test1  col3  col2
0      1   1.0   NaN
1      2   4.0   NaN
2      3   8.0   NaN
0      4   NaN   4.0
1      5   NaN   4.0
2      6   NaN   8.0

# dumps dataframes to "file.csv" in the current folder
df3.to_csv("file.csv")

最快的方法是使用csvkit特別是csvstack

cat csv_1.csv                                                                                                                                                              
id,col1,col2,col3
1,'test','dog','cat'
2,'foo','fish','rabbit'

cat csv_2.csv                                                                                                                                                              
id,col1,col2,col3
3,'bar','owl','crow'
4,'spam','eel','cow'

# To stdout
csvstack csv_1.csv csv_2.csv

id,col1,col2,col3
1,'test','dog','cat'
2,'foo','fish','rabbit'
3,'bar','owl','crow'
4,'spam','eel','cow'



# To new file.
csvstack csv_1.csv csv_2.csv > csv_1_2.csv

使用全局庫csv

import csv

header = None
new_file = []
for f in ('csv_1.csv', 'csv_2.csv'):
    with open(f, newline='') as csv_file:
        reader = csv.reader(csv_file)
        if not header:
            new_file.append(reader.__next__())
            header = True
        else:
            reader.__next__()
        for row in reader:
            new_file.append(row)

with open('csv_new_file.csv', 'w', newline='') as csv_out:
    writer = csv.writer(csv_out)
    for row in new_file:
        writer.writerow(row)

cat csv_new_file.csv

id,col1,col2,col3
1,'test','dog','cat'
2,'foo','fish','rabbit'
3,'bar','owl','crow'
4,'spam','eel','cow'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM