pandas 熊猫合并的关键错误(左连接)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29242387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Key error on pandas merge (left join)
提问by metersk
I have two dataframes below, df_purchase(1) and df_login(2)
我下面有两个数据框,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 | | |
+--------+-----+--------+------------+--------------------+-------------+--------------------------+
I am trying to join df_logins to df_purchase on age, gender and region with the following pandas code:
我正在尝试使用以下Pandas代码将 df_logins 加入 df_purchase 的年龄、性别和地区:
df = pd.merge(df_purchase, df_login[['count']],
how='left', on=['age', 'gender', 'region'])
However, I keep getting this error: KeyError: 'age'Any thoughts?
但是,我不断收到此错误:有KeyError: 'age'什么想法吗?
采纳答案by EdChum
The KeyError arises from this:
KeyError 来自于此:
df = pd.merge(df_purchase, df_login[['count']] <- this selects just count column,
how='left', on=['age', 'gender', 'region'])
You've specifically selected just a single column from df_login, you need this:
您只从 中专门选择了一个列df_login,您需要这样:
df = pd.merge(df_purchase, df_login,
how='left', on=['age', 'gender', 'region'])
I'm assuming that this is not your complete data as you have no common values in the age and region column in df_login.
我假设这不是您的完整数据,因为您在df_login.

