在python中合并公共列上的两个数据帧

2024-10-02 22:30:01 发布

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

#将单个数据集合并到一个公共字段中,即忠诚卡号(LYLTY_Card_NBR)

Customer_Data = pd.merge(Transaction_data, Purchase_behaviour, on=Transaction_data['LYLTY_CARD_NBR'])

但我收到了这个错误信息


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-47-13c21c7d1662> in <module>
      1 #Merging the individual datasets on a common field, the Loyalty Card Number (LYLTY_CARD_NBR)
----> 2 Customer_Data = pd.merge(Transaction_data, Purchase_behaviour, on=Transaction_data['LYLTY_CARD_NBR'])

~\anaconda3\lib\site-packages\pandas\core\reshape\merge.py in merge(left, right, how, on, left_on, right_on, left_index, right_index, sort, suffixes, copy, indicator, validate)
     72     validate=None,
     73 ) -> "DataFrame":
---> 74     op = _MergeOperation(
     75         left,
     76         right,

~\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)
    650             self.right_join_keys,
    651             self.join_names,
--> 652         ) = self._get_merge_keys()
    653 
    654         # validate the merge keys dtypes. We may need to coerce

~\anaconda3\lib\site-packages\pandas\core\reshape\merge.py in _get_merge_keys(self)
    994                     else:
    995                         if rk is not None:
--> 996                             right_keys.append(right._get_label_or_level_values(rk))
    997                             join_names.append(rk)
    998                         else:

~\anaconda3\lib\site-packages\pandas\core\generic.py in _get_label_or_level_values(self, key, axis)
   1561             values = self.axes[axis].get_level_values(key)._values
   1562         else:
-> 1563             raise KeyError(key)
   1564 
   1565         # Check for duplicates

KeyError: 0          47142
1          55073
2          55073
3          58351
4          68193
           ...  
264831    242159
264832    244213
264833    256018
264834    257079
264835    265006
Name: LYLTY_CARD_NBR, Length: 264836, dtype: int64

Tags: inselfrightdatagetonmergekeys
1条回答
网友
1楼 · 发布于 2024-10-02 22:30:01

如果交易数据和购买行为数据框中都存在列名“LYLTY\u CARD\u NBR”,则选项on=的值应仅为列名

Customer_Data = pd.merge(Transaction_data, Purchase_behaviour, on='LYLTY_CARD_NBR')

我建议您注意要执行的合并类型: 如何{'left','right','outer','inner','cross'},默认为'inner'

Customer_Data = pd.merge(Transaction_data, Purchase_behaviour, on='LYLTY_CARD_NBR', how='inner')

请参阅参考资料:https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html

相关问题 更多 >