Python-合并两个数据框问题

时间:2019-01-26 03:24:23

标签: python pandas

我正在尝试使用pandas合并功能合并python中的两个数据集,但出现一些无法解决的怪异错误。你能看一下让我知道我在做什么错吗?

这是第一个数据集的列的列表和数据的类型: FIFA数据集

Nationality        object
Name               object
Age                 int64
Overall             int64
Potential           int64
Club               object
Value              object
Wage               object
Preferred Foot     object
Weak Foot         float64
Skill Moves       float64
Position           object
Jersey Number     float64
Joined             object
Weight             object
dtype: object

这是第二个数据集: GDP数据集:

Nationaility     object
Population       float64
GDP              float64
dtype: object

这是我使用的代码:

 master_df = pd.merge(fifa_subset, gdp_subset, on = "Nationaility", how = 
 "right", suffixes=('_',''))

我收到此错误:

 C:\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py in 
 __init__(self, left, right, how, on, left_on, right_on, axis, left_index, 
 right_index, sort, suffixes, copy, indicator, validate)
 549         (self.left_join_keys,
 550          self.right_join_keys,
 -->  551          self.join_names) = self._get_merge_keys()
 552 
 553         # validate the merge keys dtypes. We may need to coerce

 C:\Anaconda3\lib\site-packages\pandas\core\reshape\merge.py in 
 _get_merge_keys(self)
 855                             right_keys.append(
 856                                 right._get_label_or_level_values(
 -->  857                                     rk, stacklevel=stacklevel))
 858                         else:
 859                             # work-around for 
 merge_asof(right_index=True)

 C:\Anaconda3\lib\site-packages\pandas\core\generic.py in 
 _get_label_or_level_values(self, key, axis, stacklevel)
 1380             values = self.axes[axis].get_level_values(key)._values
 1381         else:
 ->   1382             raise KeyError(key)
 1383 
 1384         # Check for duplicates

 KeyError: 'Nationaility'

1 个答案:

答案 0 :(得分:1)

正如@ALollz上面评论的那样,如果您决定保留列名不变,那么这里是完整的代码。

master_df = pd.merge(fifa_subset, gdp_subset, left_on = "Nationality", right_on = "Nationaility", how = "right", suffixes=('_',''))