您好,我对 Pandas 的了解不够,无法给您答案,但我可以使用 csv 模块给您答案。
我不确定我生成的随机数据是否与您的数据匹配,很难:
import os.path
import random
import datetime
import csv
import glob
output_directory = "/Users/Files/Daily"
def create_files_with_random_values(nb_files, nb_rows_in_output_file):
"""Create for us, a number of files with random values"""
for file_number_for_name in range(nb_files):
random_content_filename = os.path.join(output_directory, "{}.csv".format(file_number_for_name + 1))
# Choose a random date after July 14th 2017
start_date = datetime.datetime(2017, 7, 14, 2,0,0) + datetime.timedelta(random.randrange(23))
with open(random_content_filename, 'w', newline='') as random_content_file:
random_writer = csv.writer(random_content_file)
# Write the first row
random_writer.writerow(('', 'close', 'high', 'low',
'open', 'time', 'volumefrom',
'volumeto', 'timestamp'))
# Write the rest of the rows using a generator expression
random_writer.writerows((x,
round(random.uniform(0, 2), 2),
round(random.uniform(0, 2), 2),
round(random.uniform(0, 2), 2),
"".join(random.choices("0123456789", k=10)),
round(random.uniform(0, 100), 2),
round(random.uniform(0, 100), 2),
(start_date + datetime.timedelta(x)).isoformat(' ')
)
for x in range(nb_rows_in_output_file)
)
create_files_with_random_values(30, 25)
output_filename = os.path.join(output_directory, "output.csv")
file_finder_pattern = os.path.join(output_directory, "*.csv")
with open(output_filename, "w", newline='') as output_file:
output_writer = csv.writer(output_file)
output_writer.writerow(('Filename', 'time'))
# Create a list containing couples containing the original file name
# and the first part of the file name (without its path)
files_wanted = [(x, os.path.splitext(os.path.basename(x))[0]) for x in glob.iglob(file_finder_pattern)
if x != output_filename]
# Sort that list on the first part of the file name (without its path)
# using a lambda function
files_wanted.sort(key=lambda x: int(x[1]))
for (input_filename, first_part_filename) in files_wanted:
with open(input_filename, "r", newline='') as input_file:
input_reader = csv.reader(input_file)
next(input_reader) # skip the header and don't keep its value
first_data_row = next(input_reader) # get row
# Write the first part of the file name (without its path) and
# the time component of the first row of this file
output_writer.writerow((first_part_filename, first_data_row[4]))
我的就寝时间已经过去了,所以如果这不是正确的答案,你将不得不提供更多关于你的输入数据和你想要的输出的细节。