簡體   English   中英

如何根據需要將多個 csv 文件與 Python 合並?

[英]How can I merge multiple csv files with Python as I want?

我有一些 csv 文件用於我的作業。 我想將它們組合起來,如下例所示。 但我不知道該怎么做。

exp1.csv

"DATE","NOW","OPEN","HIGH","LOW","Hac.","VOL %"
"01.09.2019","23,78","25,54","25,54","23,78","-","-7,04%"
"25.08.2019","25,58","23,96","26,00","23,56","2,14M","4,07%"

exp2.csv

"DATE","NOW","OPEN","HIGH","LOW","Hac.","VOL %"
"01.09.2019","4,16","4,15","4,23","4,12","-","0,73%"
"25.08.2019","4,13","4,05","4,19","4,03","6,48M","1,98%"

我想像這樣合並 2 個文件。 我只想得到 VOL% 列。


"DATE","Exp1","Exp2"
"01.09.2019","-7,04%","0,73%"
"25.08.2019","4,07%","1,98%"

謝謝大家:)我找到了這樣的解決方案並應用了它。

import glob
import os
import pandas.io

path =r'/Users/baris/Documents/Files/'
all_files = glob.glob(os.path.join(path, "*.csv"))
df_from_each_file = (pandas.read_csv(f) for f in all_files)
concatenated_df = pandas.concat(df_from_each_file, axis=1)
concatenated_df_clean = (concatenated_df.drop('DATE',1).drop('NOW',1).drop('OPEN',1).drop('HIGH.',1).drop('Low',1).drop('Hac.',1)

df_dates_file = pandas.read_csv('/Users/baris/Documents/Files/Exp1.csv')
df_date_export = concatenated_df.iloc[:, 0]

final_result = pandas.concat([df_date_export,concatenated_df_clean], axis=1)
print(final_result)


import csv

with open('Exp1.csv', 'r') as f1:
    csv_reader = csv.reader(f1, delimiter=',')
    lines1 = [row for row in csv_reader]

with open('Exp2.csv', 'r') as f2:
    csv_reader = csv.reader(f2, delimiter=',')
    lines2 = [row for row in csv_reader]

del lines1[0]
del lines2[0]
with open('output.csv', 'w+') as output_file:
    output_file.write('"DATE","Exp1","Exp2"\n')
    for index, _ in enumerate(lines1):
        date = lines1[index][0]
        vol1 = lines1[index][6]
        vol2 = lines2[index][6]
        output_file.write(f'"{date}","{vol1}","{vol2}"\n')

這假設如下:

  • VOL %總是會在第 7 列(就像在你的例子中)
  • DATE總是在第一列(就像你的例子一樣)
  • Exp1.csvExp2.csv的行數總是相同的
  • Exp1.csvExp2.csv"DATE"將始終相同

閱讀有關 CSV 模塊的更多信息: https : //docs.python.org/3/library/csv.html

您可以使用 pandas 包來讀取和保存 csv。 但是,在合並 csv 文件時無法刪除列,但可以保存所需的列,請查看下面的代碼。 將 csv 文件名和列名替換為您的。

import pandas as pd

# create list of files you want to merge
all_filenames = ['test.csv','test1.csv']

# use pandas concat function to merge csv's
combined_csv = pd.concat([pd.read_csv(f) for f in all_filenames ])

# export the csv
combined_csv.to_csv( "combined_csv.csv", index=False, encoding='utf-8-sig',columns=['test1'])

嘗試這樣的事情:

df = pd.read_csv('Exp1.csv')

df1 = pd.read_csv('Exp2.csv')

df['DATE'] = pd.to_datetime(df['DATE'])
df1['DATE'] = pd.to_datetime(df['DATE'])

final_df = pd.merge(df[['DATE', 'VOL %']], df1[['DATE', 'VOL %']], on='DATE')

print(final_df)
      DATE VOL %_x VOL %_y
2019-01-09  -7,04%   0,73%
2019-08-25   4,07%   1,98%

使用 csv 模塊。

https://docs.python.org/3/library/csv.html

閱讀本教程:

https://realpython.com/python-csv/

像這樣的事情將完成工作:(教育代碼)

import io
import csv

target = {}

file_one_string =\
""""DATE","NOW","OPEN","HIGH","LOW","Hac.","VOL %"
"01.09.2019","23,78","25,54","25,54","23,78","-","-7,04%"
"25.08.2019","25,58","23,96","26,00","23,56","2,14M","4,07%"
"""
file_two_string = \
""""DATE","NOW","OPEN","HIGH","LOW","Hac.","VOL %"
"01.09.2019","4,16","4,15","4,23","4,12","-","0,73%"
"25.08.2019","4,13","4,05","4,19","4,03","6,48M","1,98%"
"""


with io.StringIO(file_one_string) as file_one:
    csv_reader = csv.DictReader(file_one,delimiter=',',quotechar='"')
    for row in csv_reader:
        if 'VOL %' in row:
            target[row['DATE']] ={'Exp1': row['VOL %']}

with io.StringIO(file_two_string) as file_two:
    csv_reader = csv.DictReader(file_two,dialect="excel")
    for row in csv_reader:
        if row['DATE'] in target:
            target[row['DATE']]['Exp2'] = row['VOL %']
        else:
            print('Missing DATE {} in file_two'.format(row['DATE']))
    lines2 = [row for row in csv_reader]


with io.StringIO() as output_file:
    fieldnames = ['DATE', 'Exp1', 'Exp2']
    csv_writer = csv.DictWriter(output_file, fieldnames=fieldnames)
    csv_writer.writeheader()
    for key, value in target.items():
        csv_writer.writerow({
            'DATE': key,
            'Exp1': value['Exp1'],
            'Exp2': value['Exp2']
        })

    print(output_file.getvalue())

暫無
暫無

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

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