Python Pandas 匹配 2 个文件中的 2 列以填充文件中的值

Python Pandas Match 2 columns from 2 files to fill values in a file

请帮助填写“file1.csv”(每日数据)和“file2.csv”(每周数据)的值。

下面是“file1.csv”(每日数据)::

date,value,week_num,year,fill_col_1,fill_col_2
01-01-2018,1763.95,1,2018,,
02-01-2018,1736.2,1,2018,,
03-01-2018,1741.1,1,2018,,
04-01-2018,1779.95,1,2018,,
05-01-2018,1801.1,1,2018,,
08-01-2018,1816,2,2018,,
09-01-2018,1823,2,2018,,
10-01-2018,1812.05,2,2018,,
11-01-2018,1825,2,2018,,
12-01-2018,1805,2,2018,,

下面是“file2.csv”(每周数据)::

date,value,week_num,year,fill_col_1,fill_col_2
07-01-2018,1764.46,1,2018,1768.953333,1756.542153
14-01-2018,1816.21,2,2018,1811.966667,1801.030007

"file1.csv"需要填写的2栏是"fill_col_1""fill_col_2" ,通过匹配 "week_num""year" from "file2.csv".

是否有某种方法可以比较 2 个文件中的 2 列,考虑或不考虑两个文件中明显不同的索引(“日期”)。

(如果您看到,“file2.csv”(每周)是使用 pandas 中的 'resample' 函数派生的,基于(每日值)“file1.csv".. 但现在我无法 join/concatentate 基于多个 columns/conditions 匹配的 2 个文件。)

预期的output/result,需要如下:

date,value,match_col_1,match_col_2,fill_col_1,fill_col_2
01-01-2018,1763.95,1,2018,1768.953333,1756.542153
02-01-2018,1736.2,1,2018,1768.953333,1756.542153
03-01-2018,1741.1,1,2018,1768.953333,1756.542153
04-01-2018,1779.95,1,2018,1768.953333,1756.542153
05-01-2018,1801.1,1,2018,1768.953333,1756.542153
08-01-2018,1816,2,2018,1811.966667,1801.030007
09-01-2018,1823,2,2018,1811.966667,1801.030007
10-01-2018,1812.05,2,2018,1811.966667,1801.030007
11-01-2018,1825,2,2018,1811.966667,1801.030007
12-01-2018,1805,2,2018,1811.966667,1801.030007

我试过的代码如下:(这绝对是我现在看到的菜鸟思维过程)

df_daily = pd.read_csv("file1.csv")
df_weekly = pd.read_csv("file2.csv")
df_weekly.loc[df_weekly["year"]]==df_daily.loc[df_daily["year"]] & df_weekly.loc[df_weekly["week_num"]]==df_daily.loc[df_daily["week_num"]]

出现错误:KeyError: "None of [Int64Index([2018, 2018, 2018, 2018, 2018, 2018, 2018, ],\n dtype='int64', length=8)] 在 [index]"

尝试 pandas.DataFrame.merge 功能:

import pandas as pd

df_daily = pd.read_csv('1.csv').iloc[:,:4]
df_weekly = pd.read_csv('2.csv').iloc[:,2:]
print(df_daily.merge(df_weekly,on=['week_num','year'],how='left'))