使用Python代码按日期提取数据&;按日期另存为单独的csv
我必须按日期提取数据,并为每个不同的日期保存为单独的csv: “时间”以这种格式(2018-03-26T16:09:10.024101278Z)在CSV文件的一列中给出 此数据集在不同的时间内获取了超过10万行。 “我试过制作一个数据框”“”“列名:(名称时间id ddr版本读数)以供参考” “得到了这样的日期列表([datetime.Date(2018,3,26),datetime.Date(2018,3,31)]) 我收到了如下所示的空数据帧使用Python代码按日期提取数据&;按日期另存为单独的csv,python,pandas,datetime,Python,Pandas,Datetime,我必须按日期提取数据,并为每个不同的日期保存为单独的csv: “时间”以这种格式(2018-03-26T16:09:10.024101278Z)在CSV文件的一列中给出 此数据集在不同的时间内获取了超过10万行。 “我试过制作一个数据框”“”“列名:(名称时间id ddr版本读数)以供参考” “得到了这样的日期列表([datetime.Date(2018,3,26),datetime.Date(2018,3,31)]) 我收到了如下所示的空数据帧 name time id ddr
name time id ddr version readings Dates Time
Dates
如何通过字符串进行比较
Date_set = dataset_CT.loc[dataset_CT.Dates=='2018-03-26']
如果不工作,请尝试更改:
对于没有时间的日期时间:
dataset_CT['Dates'] = pd.to_datetime(dataset_CT['time']).dt.floor('d')
Date_set = dataset_CT.loc[dataset_CT.Dates=='2018-03-26']
当您使用默认参数读取输入时,我将假设您有逗号(
,
)作为分隔符和一个标题行。这里不需要IMHO熊猫。一次读取一行文件并将其写入与日期对应的csv文件就足够了
注意事项:将标题添加到每个输出csv文件,并为每个新日期创建一个新的输出文件。带有自定义默认函数的collections.defaultdict
足以满足这两个要求
以下代码读取名为“out_1.csv”
的输入csv文件,并将其内容写入名为out_2018-03-26.csv的一组文件中,日期为输出文件中所有行的日期:
with open("out_1.csv") as fdin:
def get_defaults():
"""returns a pair (csv_writer, file_object) for date dat initialized with header"""
filename = 'out{}.csv'.format(dat)
fd = open(filename, "w", newline='')
fd.write(header)
return (csv.writer(fd), fd)
outfiles = collections.defaultdict(get_defaults)
rd = csv.reader(fdin)
header = next(fdin) # store the header to later initialize output files
for row in rd:
dat = row[1][:10] # extract the date
wr = outfiles[dat][0]
wr.writerow(row) # and write the row to the appropriate output file
# close the output files
for i in outfiles:
outfile[i][1].close()
再考虑一下,上面的代码可能会保留太多打开的文件。以下是一个改进版本,它仅为最近遇到的3个日期(已测试)保留打开的文件:
@anky_91-这取决于熊猫的版本,在熊猫0.24中,+两者都应该起作用。
dataset_CT['Dates'] = pd.to_datetime(dataset_CT['time']).dt.date
Date_set = dataset_CT.loc[dataset_CT.Dates=='2018-03-26']
dataset_CT['Dates'] = pd.to_datetime(dataset_CT['time']).dt.floor('d')
Date_set = dataset_CT.loc[dataset_CT.Dates=='2018-03-26']
with open("out_1.csv") as fdin:
def get_defaults():
"""returns a pair (csv_writer, file_object) for date dat initialized with header"""
filename = 'out{}.csv'.format(dat)
fd = open(filename, "w", newline='')
fd.write(header)
return (csv.writer(fd), fd)
outfiles = collections.defaultdict(get_defaults)
rd = csv.reader(fdin)
header = next(fdin) # store the header to later initialize output files
for row in rd:
dat = row[1][:10] # extract the date
wr = outfiles[dat][0]
wr.writerow(row) # and write the row to the appropriate output file
# close the output files
for i in outfiles:
outfile[i][1].close()
with open("out_1.csv") as fdin:
cache = collections.deque()
seen = set()
def get_defaults():
"""returns a pair (csv_writer, file_object) for date dat initialized with header"""
filename = 'out{}.csv'.format(dat)
fd = open(filename, 'a' if dat in seen else 'w', newline='')
if 0 == fd.tell(): # file is currently empty: write header
fd.write(header)
ret = (csv.writer(fd), fd)
cache.append(dat)
seen.add(dat)
if len(cache) > 3: # only keep 3 open files
old = cache.popleft()
print("Closing", old)
outfiles[old][1].close()
del outfiles[old]
return ret
outfiles = collections.defaultdict(get_defaults)
rd = csv.reader(fdin)
header = next(fdin) # store the header to later initialize output files
for row in rd:
dat = row[1][:10] # extract the date
wr = outfiles[dat][0]
wr.writerow(row) # and write the row to the appropriate output file
# close the currently opened output files
for i in outfiles:
outfiles[i][1].close()