我有 2 个数据框:
restaurant_ids_dataframe
Data columns (total 13 columns):
business_id 4503 non-null values
categories 4503 non-null values
city 4503 non-null values
full_address 4503 non-null values
latitude 4503 non-null values
longitude 4503 non-null values
name 4503 non-null values
neighborhoods 4503 non-null values
open 4503 non-null values
review_count 4503 non-null values
stars 4503 non-null values
state 4503 non-null values
type 4503 non-null values
dtypes: bool(1), float64(3), int64(1), object(8)`
和
餐厅评论框架
Int64Index: 158430 entries, 0 to 229905
Data columns (total 8 columns):
business_id 158430 non-null values
date 158430 non-null values
review_id 158430 non-null values
stars 158430 non-null values
text 158430 non-null values
type 158430 non-null values
user_id 158430 non-null values
votes 158430 non-null values
dtypes: int64(1), object(7)
我想加入这两个 DataFrame,使用 pandas 中的 DataFrame.join() 命令将它们变成一个数据帧。
我尝试了以下代码行:
#the following line of code creates a left join of restaurant_ids_frame and restaurant_review_frame on the column 'business_id'
restaurant_review_frame.join(other=restaurant_ids_dataframe,on='business_id',how='left')
但是当我尝试这样做时,出现以下错误:
Exception: columns overlap: Index([business_id, stars, type], dtype=object)
我对熊猫很陌生,就执行连接语句而言,我不知道我做错了什么。
任何帮助将非常感激。
原文由 anonuser0428 发布,翻译遵循 CC BY-SA 4.0 许可协议
您可以使用 merge 将两个数据帧合并为一个:
where on 指定存在于两个数据框中的字段名称以加入,以及 如何 定义其内部/外部/左/右连接,外部使用“来自两个框架的键的联合(SQL:完全外部连接)”。由于您在两个数据框中都有“星”列,因此默认情况下会在组合数据框中创建两列 star_x 和 star_y。正如@DanAllan 提到的 join 方法,您可以通过将其作为 kwarg 传递来修改合并的后缀。默认为
suffixes=('_x', '_y')
。如果你想做类似star_restaurant_id
和star_restaurant_review
的事情,你可以这样做:此 链接 中详细解释了这些参数。