Python 我想根据日期和时间合并2张excel工作表?

Python 我想根据日期和时间合并2张excel工作表?,python,Python,我有两个数据帧,其中第一个数据帧包含两列,即日期和时间。 第二个数据框包含多个列,即日期、时间、打开、高、低、关闭。 我想根据日期和时间将第二个数据帧合并到第一个数据帧。下面是代码 第一数据帧- Date Time 0 2015-02-02 09:15:00 1 2015-02-02 09:16:00 2 2015-02-02 09:17:00 3 2015-02-02 09:18:00 4 2015-02-02 09:19:00 ... ... ..

我有两个数据帧,其中第一个数据帧包含两列,即日期和时间。 第二个数据框包含多个列,即日期、时间、打开、高、低、关闭。 我想根据日期和时间将第二个数据帧合并到第一个数据帧。下面是代码

第一数据帧-

    Date    Time
0   2015-02-02  09:15:00
1   2015-02-02  09:16:00
2   2015-02-02  09:17:00
3   2015-02-02  09:18:00
4   2015-02-02  09:19:00
... ... ...
447380  2019-12-10  15:25:00
447381  2019-12-10  15:26:00
447382  2019-12-10  15:27:00
447383  2019-12-10  15:28:00
447384  2019-12-10  15:29:00
open    high     low   close  volume Symbol    Date     Time
121.00  121.00  121.00  121.00  0     ABFRL 2015-02-02   09:15:00
123.00  123.00  120.20  120.20  0     ABFRL 2015-02-02   09:16:00
120.20  120.20  120.20  120.20  0     ABFRL 2015-02-02   09:17:00
120.20  120.20  120.20  120.20  0     ABFRL 2015-02-02   09:18:00
120.20  120.20  120.20  120.20  0     ABFRL 2015-02-02   09:19:00
第二数据帧-

    Date    Time
0   2015-02-02  09:15:00
1   2015-02-02  09:16:00
2   2015-02-02  09:17:00
3   2015-02-02  09:18:00
4   2015-02-02  09:19:00
... ... ...
447380  2019-12-10  15:25:00
447381  2019-12-10  15:26:00
447382  2019-12-10  15:27:00
447383  2019-12-10  15:28:00
447384  2019-12-10  15:29:00
open    high     low   close  volume Symbol    Date     Time
121.00  121.00  121.00  121.00  0     ABFRL 2015-02-02   09:15:00
123.00  123.00  120.20  120.20  0     ABFRL 2015-02-02   09:16:00
120.20  120.20  120.20  120.20  0     ABFRL 2015-02-02   09:17:00
120.20  120.20  120.20  120.20  0     ABFRL 2015-02-02   09:18:00
120.20  120.20  120.20  120.20  0     ABFRL 2015-02-02   09:19:00
主代码

import pandas as pd
import os
import datetime

file = pd.read_excel("D:\\Aman\\bn trading.xlsx")
file.info()


total = os.listdir("D:\\Aman\\Data\\Stocks Data 2015-2019 (1 min)")
for i in total:
    stockfile = pd.read_csv("D:\\Aman\\Data\\Stocks Data 2015-2019 (1 min)\\"+i)
    stockfile["Date"] = pd.to_datetime(stockfile["Date"])
    stockfile.info()

    final = pd.merge(file,stockfile[["close","Date","Time"]],how = "left",on = ("Date","Time") )

final
因此,上面的代码给了我空数据帧。

将下面的更改为

    final = pd.merge(file,stockfile[["close","Date","Time"]],how = "outer",on = ["Date","Time"] )


这将为集合中左侧或右侧不匹配的项目显示NaN。

我想以“左”数据框作为日期和时间的基础。它们是相同的日期时间格式还是字符串格式?请按此转换。`````````文件['Date']=file['Date'].astype(str)file['Time']=file['Time'].astype(str)stockfile['Date']=stockfile['Date'].astype(str)stockfile['Time']=stockfile['Time'].astype(str)````然后使用
pd.merge(file,stockfile[[close”,“Date”,“Time”]],how=“left”,on=[“Date”,“Time”])
它可以工作。谢谢如何将列名设置为第二个数据帧的文件名(从该数据帧开始执行关闭操作)?将其添加为列名是没有用的,但添加一个字段作为源并将其设置为文件名。````````文件['source']=“D:\\Aman\\bn trading”``如果您觉得这很有用,也可以保留一个向上投票,以便其他人下次搜索时可以找到相同的答案。