[英]How to merge .csv files with python?
我們必須合並很多 CSV 文件,但沒有找到任何工作方法(通過 MS Excel,通過 Python & Z251D2BBBFE9ADCB784569 ... 所有文件的結構都與您在所附照片中看到的相同。 無需更改內容,只需將所有文件的所有列添加到一個大型 CSV 中。 期待你的想法! .csv文件的結構
import os
import glob
import pandas as pd
# Set me!
# WORKING_DIR = "/mydir"
# or leave blank to act on current folder
WORKING_DIR = ""
OUTPUT = "combined.csv"
if WORKING_DIR:
# set working directory
os.chdir(WORKING_DIR)
# find all csv files in the folder
suffix = '.csv'
all_filenames = [
p for p in glob.glob('*{}'.format(suffix))
if p != OUTPUT
]
final = pd.concat((pd.read_csv(f) for f in all_filenames), axis=1)
# export to csv
final.to_csv(OUTPUT, index=False, encoding='utf-8-sig')
基於https://github.com/ekapope/Combine-CSV-files-in-the-folder/blob/master/Combine_CSVs.py和 Bernardo Costa 提供的答案。
您需要安裝 pandas 才能正常工作。
一種可能的方法:
import pandas as pd
import os
# File paths:
all_files_paths = [] # empty list to store your .csv file names
for i in os.listdir(r'C:\\Users\\John'): # directory where all your .csv file are
if i[:5] == 'shoes': # load only files starting with for example "shoes".
all_files_paths.append(i)
list_of_dataframes = [] # empty list to store the dataframes of your files
for i in range(len(all_files_paths)):
list_of_dataframes.append(pd.read_csv(os.path.join('C:\\Users\\John', all_files_paths[i])))
final_dataframe = pd.concat(list_of_dataframes, axis=1) # concatenating the different .csv files.
加入、連接或合並時還有更多選項。csv 文件值得探索。
將所有要合並的 csv 文件放到一個空目錄中。 嘗試運行以下代碼並將 dir_path 設置為該目錄:
import os
import pandas as pd
dir_path = "path_to_directory_containing_all_csv_files_goes_here"
all_filenames = os.listdir(dir_path)
combined_csv = pd.concat([pd.read_csv(dir_path+f) for f in all_filenames],axis=1)
combined_csv.to_csv('combined.csv', index=False )
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.