[英]Read and concat csv files from past 14 days in python pandas
I need to write a script that read all csv files with specific names from past 14 days (every day in the morning), but when I do concat this gives me a little cube (in jupyter-notebook), and that sign there is nothing. 我需要编写一个脚本来读取过去14天(每天每天)中具有特定名称的所有csv文件,但是当我进行concat操作时,这会给我一个小立方体(在jupyter-notebook中),并且该符号没有任何内容。
def get_local_file(pdate, hour, path='/data/'):
"""Get date+hour processing file from local drive
:param pdate: str Processing date
:param hour: str Processing hour
:param path: str Path to file location
:return: Pandas DF Retrieved DataFrame
"""
sdate = pdate + '-' + str(hour)
for p_file in os.listdir(path):
if fnmatch.fnmatch(p_file, 'ABC_*'+sdate+'*.csv'):
return path+p_file
def get_files(pdate, path='/data/'):
hours = [time(i).strftime('%H') for i in range(24)]
fileList=[]
for hour in hours:
fileList.append(get_local_file(pdate, hour))
return fileList
end_datetime = datetime.combine(date.today(), time(0, 0, 0))
proc_datetime = end_datetime - timedelta(days=14)
while proc_datetime <= end_datetime:
proc_datetime += timedelta(days=1)
a = get_files(str(proc_datetime.date()).replace('-', '_'))
frame = pd.DataFrame()
list_ = []
for file_ in a:
if file_ != None:
df = pd.read_csv(file_,index_col=None, header=0, delimiter=';')
list_.append(df)
frame = pd.concat(list_)
I'm pretty sure that is possible to make code from while loop and below much simpler, but have no idea how to do it. 我敢肯定,可以从while循环及其下面简化代码,但不知道如何做。
If you want to create a single dataframe from a bunch of .csv
files you can do it this way: 如果要从一堆
.csv
文件创建单个数据框,则可以通过以下方式进行操作:
I did not check if your handling of dates and filenames is correct but here are the relevant changes to your code regarding the concatenation part: 我没有检查您对日期和文件名的处理是否正确,但是以下是有关串联部分的代码的相关更改:
end_datetime = datetime.combine(date.today(), time(0, 0, 0))
proc_datetime = end_datetime - timedelta(days=14)
list_ = []
while proc_datetime <= end_datetime:
proc_datetime += timedelta(days=1)
a = get_files(str(proc_datetime.date()).replace('-', '_'))
for file_ in a:
if file_ != None:
df = pd.read_csv(file_, index_col=None, header=0, delimiter=';')
list_.append(df)
frame = pd.concat(list_)
import pandas
import glob
csvFiles = glob.glob(path + "/data/*.csv")
list_ = []
for file in csvFiles:
if ((datetime.combine(date.today(), time(0, 0, 0)) - datetime(*map(int, file.split("-")[1].split("_")))).days < 14)
df = pandas.read_csv(file, index_col=None, header=0, delimiter=';')
list_.append(df_f)
frame = pandas.concat(list_, ignore_index=True)
frame.to_csv("Appended File.csv")
Assuming the file path doesnot have any hyphen(-) characters in it. 假定文件路径中没有任何连字符(-)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.