gpt4 book ai didi

python-3.x - Pandas 合并返回空数据框

转载 作者:行者123 更新时间:2023-12-02 19:59:15 24 4
gpt4 key购买 nike

我正在从两个不同的 csv 加载两个数据帧,并尝试加入它们,但由于某种原因 pd.merge 没有加入数据并返回空数据帧。我尝试过更改数据类型,但仍然没有任何结果。感谢任何帮助。

示例 mon_apps 数据帧:

app_cnts | year | month
16634 | 2018 | 8
9636 | 2019 | 2
17402 | 2017 | 8
17472 | 2017 | 11
15689 | 2018 | 6

示例mon_spend数据帧:

channel  | month   | year  | spend
FB | 1 | 2017 | 0
FB | 2 | 2017 | 0
FB | 3 | 2017 | 0
FB | 4 | 2017 | 0
FB | 5 | 2017 | 0

我更改了数据类型(只是为了确保这不是问题),如下所示:

mon_spend[['month', 'year', 'spend']] = mon_spend[['month', 'year', 'spend']].astype(np.int64)
mon_spend['channel'] = mon_spend['channel'].astype(str)
mon_apps = mon_apps.astype(np.int64)

我检查数据类型:

mon_spend
channel object
month int64
year int64
spend int64
dtype: object

mon_apps
app_cnts int64
year int64
month int64
dtype: object

我使用 pd.merge 加入,如下所示:

pd.merge(mon_apps[['app_cnts', 'year', 'month']], mon_spend, left_on = ["year", "month"], right_on = ["year", "month"])

感谢任何帮助。谢谢。

更多数据

channel month year spend




FB 2017 1 0


FB 2017 2 0


FB 2017 3 0


FB 2017 4 0


FB 2017 5 0


FB 2017 6 0


FB 2017 7 52514


FB 2017 8 10198


FB 2017 9 25408


FB 2017 10 31333


FB 2017 11 128071


FB 2017 12 95160


FB 2018 1 5001


FB 2018 2 17929


FB 2018 3 84548


FB 2018 4 16414


FB 2018 5 28282


FB 2018 6 38430


FB 2018 7 58757


FB 2018 8 120722


FB 2018 9 143766


FB 2018 10 68400


FB 2018 11 66984


FB 2018 12 58228

更多信息

print (mon_spend[["year", "month"]].sort_values(["year", "month"]).drop_duplicates().values.tolist())
[[2017, 1], [2017, 2], [2017, 3], [2017, 4], [2017, 5]]

print (mon_apps[["year", "month"]].sort_values(["year", "month"]).drop_duplicates().values.tolist())

[[2017, 8], [2017, 11], [2018, 6], [2018, 8], [2019, 2]]









[[1, 2017], [1, 2018], [1, 2019], [2, 2017], [2, 2018], [2, 2019], [3, 2017], [3, 2018], [4, 2017], [4, 2018], [5, 2017], [5, 2018], [6, 2017], [6, 2018], [7, 2017], [7, 2018], [8, 2017], [8, 2018], [9, 2017], [9, 2018], [10, 2017], [10, 2018], [11, 2017], [11, 2018], [12, 2017], [12, 2018]]
[[2017, 1], [2017, 2], [2017, 3], [2017, 4], [2017, 5], [2017, 6], [2017, 7], [2017, 8], [2017, 9], [2017, 10], [2017, 11], [2017, 12], [2018, 1], [2018, 2], [2018, 3], [2018, 4], [2018, 5], [2018, 6], [2018, 7], [2018, 8], [2018, 9], [2018, 10], [2018, 11], [2018, 12], [2019, 1], [2019, 2], [2019, 3]]




Out[18]:

[[2017, 8], [2017, 11], [2018, 6], [2018, 8], [2019, 2]]

最佳答案

您要合并的文件中的月份和年份会交换,请尝试在合并时交换键或在合并之前重命名它们:

mon_apps.merge(complete_file, left_on=["year", "month"],right_on=['month','year'])

关于python-3.x - Pandas 合并返回空数据框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56249665/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com