我有一些用于家庭作业的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)
答案 0 :(得分:1)
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 %
始终位于第七列(如您的示例一样)DATE
始终位于第一列(如您的示例一样)Exp1.csv
和Exp2.csv
中的行数始终相同"DATE"
和Exp1.csv
中的Exp2.csv
将始终相同答案 1 :(得分:1)
您可以使用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'])
答案 2 :(得分:0)
尝试这样的事情:
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%
答案 3 :(得分:-1)
使用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())