Pandas合并(左连接)时出现键错误

2024-09-28 23:15:38 发布

您现在位置:Python中文网/ 问答频道 /正文

我在下面有两个数据框,df_purchase(1)和df_login(2)

+--------+-----+--------+------------+--------------------+-------------+--------------------------+
|        | age | gender |    ttp     |       count        | sum(amount) |          region          |
+--------+-----+--------+------------+--------------------+-------------+--------------------------+
|  49427 | 63  | M      | 824.731412 | 2                  | 25.00       | Omaha, Nebraska          |
|  28433 | 49  | M      | 1.166250   | 2                  | 41.94       | Catasauqua, Pennsylvania |
|   4162 | 29  | M      | 5.620949   | 2                  | 51.78       | Eagle Center, Iowa       |
|  18747 | 43  | M      | 153.502072 | 2                  | 23.84       | Pacific, Washington      |
|  45173 | 59  | M      | 0.027257   | 2                  | 13.98       | De Soto, Missouri        |
+--------+-----+--------+------------+--------------------+-------------+--------------------------+

+--------+-----+--------+------------+--------------------+-------------+--------------------------+
|        | age | gender | count      | region             |             |                          |
| 671766 | 84  | M      | 13900      | New York, New York |             |                          |
| 671166 | 84  | F      | 7619       | New York, New York |             |                          |
| 672209 | 85  | F      | 6483       | New York, New York |             |                          |
| 672671 | 85  | M      | 5808       | New York, New York |             |                          |
| 195201 | 34  | M      | 3817       | New York, New York |             |                          |
+--------+-----+--------+------------+--------------------+-------------+--------------------------+

我正在尝试加入dfòu登录,以dfòu购买年龄、性别和地区,熊猫代码如下:

df = pd.merge(df_purchase, df_login[['count']],
                       how='left', on=['age', 'gender', 'region'])

但是,我一直得到这个错误:KeyError: 'age' 有什么想法吗?


Tags: 数据dfnewagecountloginpurchasegender
1条回答
网友
1楼 · 发布于 2024-09-28 23:15:38

关键错误源于:

df = pd.merge(df_purchase, df_login[['count']] <- this selects just count column,
                       how='left', on=['age', 'gender', 'region'])

您只从df_login中选择了一列,您需要:

df = pd.merge(df_purchase, df_login,
                       how='left', on=['age', 'gender', 'region'])

我假设这不是您的完整数据,因为您在df_login中的age和region列中没有公共值。

相关问题 更多 >