[英]Merging csv files into one (columnwise) in Python
我有很多這樣的.csv 文件(只有一列):圖片
我想將它們合並到一個.csv 文件中,這樣每一列都將包含 csv 文件數據之一。 標題應該是這樣的(轉換為電子表格時):圖片(第一個數字是從文件名中提取的分鍾數,第二個是名稱中“export_”后面的文件名中的第一個單詞,第三個是文件的全名)。
我想在 Python 工作。 你能請人幫我嗎? 我是 Python 的新手。
非常感謝。
我試圖只加入 2 個文件,但我不知道如何在不手動寫下所有文件的情況下使用更多文件。 另外,我不知道如何從文件名中提取標題:
import pandas as pd
file_list = ['export_Control 37C 4h_Single Cells_Single Cells_Single Cells.csv', 'export_Control 37C 0 min_Single Cells_Single Cells_Single Cells.csv']
df = pd.DataFrame()
for file in file_list:
temp_df = pd.read_csv(file)
df = pd.concat([df, temp_df], axis=1)
print(df)
df.to_csv('output2.csv', index=False)
Assuming that your .csv
files they all have a header and the same number of rows, you can use the code below to put all the .csv
(single-columned) one besides the other in a single Excel worksheet.
import os
import pandas as pd
csv_path = r'path_to_the_folder_containing_the_csvs'
csv_files = [file for file in os.listdir(csv_path)]
list_of_dfs=[]
for file in csv_files :
temp=pd.read_csv(csv_path + '\\' + file, header=0, names=['Header'])
time_number = pd.DataFrame([[file.split('_')[1].split()[2]]], columns=['Header'])
file_title = pd.DataFrame([[file.split('_')[1].split()[0]]], columns=['Header'])
file_name = pd.DataFrame([[file]], columns=['Header'])
out = pd.concat([time_number, file_title, file_name, temp]).reset_index(drop=True)
list_of_dfs.append(out)
final= pd.concat(list_of_dfs, axis=1, ignore_index=True)
final.columns = ['Column' + str(col+1) for col in final.columns]
final.to_csv(csv_path + '\output.csv', index=False)
final
例如,考慮三個.csv
文件,運行上面的代碼會產生:
>>> Output (in Jupyter)
>>> Output (in Excel)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.