繁体   English   中英

如何将 2 个 csv 文件与 python 合并?

[英]How to merge 2 csv files with python?

我想将 2 个 csv 文件合并为 1 个。这两个文件有一个公共字段“时间戳”。 我想按时间戳排序记录。

这是 2 个 csv 文件的示例:

Timestamp,Frame,Index,Latitude,Longitude,Altitude,Iso,Shutter,Fnum,Ev,Ct,Color md,Focal length
2021-11-11T15:36:57.397Z,1,00:00:00,000,46.771871,-71.313676,95.244003,100,1/60.0,500,0,5551,default,280
2021-11-11T15:36:57.431Z,2,00:00:00,033,46.771871,-71.313676,95.244003,100,1/60.0,500,0,5551,default,280
2021-11-11T15:36:57.464Z,3,00:00:00,066,46.771871,-71.313676,95.244003,100,1/60.0,500,0,5551,default,280

文件 #2

Timestamp,Output,State
2022-01-10T20:58:39.710Z,5,1
2022-01-10T20:58:41.894Z,5,0

我终于为此做了一个脚本。 这里是:

# -*- coding: utf-8 -*-
# code from https://stackoverflow.com/questions/53825240/merging-csv-files-in-directory-based-on-timestamps

# This tool is,Nt specific for this project and can be used for other applications.

# This tool merge many csv files into a single output file. It's assumed they have a Timestamp
# column with ISO8601 strings in UTC timezone.

# The Timestamp is assumed to be the 1st column in the CSV files. Not checked for Timestamp 
# in other columns.

import pandas as pd

#import srtConvert
import logging

from tkinter import Tk     # from tkinter import Tk for Python 3.x
from tkinter.filedialog import askopenfilenames,asksaveasfilename

Tk().withdraw() # we don't want a full GUI, so keep the root window from appearing

# GUI asks for input files and output file
inFiles = askopenfilenames(title='Merge tool: open CSV files to merge by Timestamp', filetypes=[('CSV logs', '*.csv')]) # show an "Open" dialog box and return the path to the selected file
outFile = asksaveasfilename(title='Merge tool: save merged file as')

frames = []

# Read all the files! We assume the timezone is the same for all files
for files in inFiles:
    dataset = pd.read_csv(files, index_col=False, parse_dates=['Timestamp'])
    frames.append(dataset)

# Combine all dataframe
df = pd.concat(frames, ignore_index = True)

# Sort entries by increasing timestamps
df = df.sort_values(by='Timestamp',ascending=True)

# Write result to file
df.to_csv(outFile, index = False)

logging.info('Output was written to ' + outFile)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM