我有許多 CSV 檔案,其中只有前三列的資料。我想從每個 CSV 檔案中復制資料,然后按列順序將其粘貼到一個 CSV 檔案中。例如,來自第一個 CSV 檔案的資料進入輸出檔案的第 1,2 和 3 列。同樣,來自第二個 CSV 的資料進入同一輸出 CSV 檔案的第 4、5 和 6 列,依此類推。任何幫助將不勝感激。謝謝。
我已經嘗試了以下代碼,但它只讓我在同一列中輸出。
import glob
import pandas as pd
import time
import numpy as np
start = time.time()
Filename='Combined_Data.csv'
extension = 'csv'
all_filenames = [i for i in glob.glob('*.{}'.format(extension))]
for i in range(len(all_filenames)):
data= pd.read_csv(all_filenames[i],skiprows=23)
data= data.rename({'G1': 'CH1', 'G2': 'CH2','Dis': 'CH3'},axis=1)
data= data[['CH1','CH2','CH3']]
data= data.apply(pd.to_numeric, errors='coerce')
print(all_filenames[i])
if i == 0:
data.to_csv(Filename,sep=',',index=False,header=True,mode='a')
else:
data.to_csv(Filename,sep=',',index=False,header=False,mode='a')
end = time.time()
print((end - start),'Seconds(Execution Time)')
uj5u.com熱心網友回復:
如果您不需要為此撰寫自己的代碼,我建議您使用 GoCSV 的zip命令;它還可以處理具有不同行數的 CSV。
我有三個 CSV 檔案:
檔案1.csv
Dig1,Dig2,Dig3
1,2,3
4,5,6
7,8,9
檔案2.csv
Letter1,Letter2,Letter3
a,b,c
d,e,f
和file3.csv
RomNum1,RomNum2,RomNum3
I,II,III
當我運行時,gocsv zip file2.csv file1.csv file3.csv我得到:
Letter1,Letter2,Letter3,Dig1,Dig2,Dig3,RomNum1,RomNum2,RomNum3
a,b,c,1,2,3,I,II,III
d,e,f,4,5,6,,,
,,,7,8,9,,,
GoCSV 是為許多不同的作業系統預先構建的。
uj5u.com熱心網友回復:
以下是使用這些檔案使用 Python 的 CSV 模塊執行此操作的方法:
檔案1.csv
Dig1,Dig2,Dig3
1,2,3
4,5,6
7,8,9
檔案2.csv
Letter1,Letter2,Letter3
a,b,c
d,e,f
和file3.csv
RomNum1,RomNum2,RomNum3
I,II,III
更占用記憶體的選項
這將一次累積一個檔案的最終 CSV,并使用每個新的輸入 CSV 擴展代表最終 CSV 的串列。
#!/usr/bin/env python3
import csv
import sys
csv_files = [
'file2.csv',
'file1.csv',
'file3.csv',
]
all = []
for csv_file in csv_files:
with open(csv_file) as f:
reader = csv.reader(f)
rows = list(reader)
len_all = len(all)
# First file, initialize all and continue (skip)
if len_all == 0:
all = rows
continue
# The number of columns in all so far
len_cols = len(all[0])
# Extend all with the new rows
for i, row in enumerate(rows):
# Check to make sure all has as many rows as this file
if i >= len_all:
all.append(['']*len_cols)
all[i].extend(row)
# Finally, pad all rows on the right
len_cols = len(all[0])
for i in range(len(all)):
len_row = len(all[i])
if len_row < len_cols:
col_diff = len_cols - len_row
all[i].extend(['']*col_diff)
writer = csv.writer(sys.stdout)
writer.writerows(all)
流媒體選項
這一次讀取和寫入一行/一行。
(這基本上是來自 GoCSV 的 zip 的 Go 代碼的 Python 埠,從上面)
import csv
import sys
fnames = [
'file2.csv',
'file1.csv',
'file3.csv',
]
num_files = len(fnames)
readers = [csv.reader(open(x)) for x in fnames]
# Collect "header" lines; each header defines the number
# of columns for its file
headers = []
num_cols = 0
offsets = [0]
for reader in readers:
header = next(reader)
headers.append(header)
num_cols = len(header)
offsets.append(num_cols)
writer = csv.writer(sys.stdout)
# With all headers counted, every row must have this many columns
shell_row = [''] * num_cols
for i, header in enumerate(headers):
start = offsets[i]
end = offsets[i 1]
shell_row[start:end] = header
# Write headers
writer.writerow(shell_row)
# Expect that not all CSVs have the same number of rows; some will "finish" ahead of others
file_is_complete = [False] * num_files
num_complete = 0
# Loop a row at a time...
while True:
# ... for each CSV
for i, reader in enumerate(readers):
if file_is_complete[i]:
continue
start = offsets[i]
end = offsets[i 1]
try:
row = next(reader)
# Put this row in its place in the main row
shell_row[start:end] = row
except StopIteration:
file_is_complete[i] = True
num_complete = 1
except:
raise
if num_complete == num_files:
break
# Done iterating CSVs (for this row), write it
writer.writerow(shell_row)
# Reset for next main row
shell_row = [''] * num_cols
對于任何一個,我得到:
Letter1,Letter2,Letter3,Dig1,Dig2,Dig3,RomNum1,RomNum2,RomNum3
a,b,c,1,2,3,I,II,III
d,e,f,4,5,6,,,
,,,7,8,9,,,
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/397069.html
