1 回答
TA贡献1895条经验 获得超7个赞
只需花一点时间就可以使日期和城市保持一致。然后就是简单的左merge()
import pandas as pd
import io
import datetime as dt
CSV_1 = pd.read_csv(io.StringIO(
"""Name City Date
Examp. Bton 7/11
Nomatch Cton 10/10"""), sep="\s\s+", engine="python")
CSV_2 = pd.read_csv(io.StringIO(
"""Name_of_thing City_of_Origin Time
huh, inc. Lton, AMERICA 7/10/2020 00:00
THE EXAMPLE, LLC Bton, USA 7/11/2020 00:00"""), sep="\s\s+", engine="python")
# need to make dates consistent and joinable
# need to pull city out of City_of_origin
CSV_3 = CSV_1.assign(
datekey=pd.to_datetime(CSV_1["Date"]+f"/{dt.date.today().year}")
).merge(
CSV_2.assign(
datekey=pd.to_datetime(CSV_2["Time"]),
City=lambda dfa: dfa["City_of_Origin"].str.extract("([A-Za-z]*)")
),
on=["datekey","City"],
how="left"
).drop(columns="datekey")
print(CSV_3.to_string())
输出
Name City Date Name_of_thing City_of_Origin Time
0 Examp. Bton 7/11 THE EXAMPLE, LLC Bton, USA 7/11/2020 00:00
1 Nomatch Cton 10/10 NaN NaN NaN
添加回答
举报