当前位置: 代码迷 >> python >> 具有非唯一联接键的Featuretools关系
  详细解决方案

具有非唯一联接键的Featuretools关系

热度:36   发布时间:2023-07-16 09:37:24.0

假设我有两个表,一个表包含有关具有字段customer_id的客户的元数据,以及一个事件表,其中记录了具有字段customer_iddate网站点击流事件。 显然,第二个表可能具有多个非唯一事件(不幸的是,日期实际上只是一个日期,而不是时间戳)。

尝试创建它失败并显示:

Index is not unique on dataframe (Entity transactions)

我怎样才能使其变得独特或使其起作用?

如果您的表没有可用作唯一索引的列,则可以让Featuretools自动创建一个。 调用EntitySet.entity_from_dataframe(...)只需将数据make_index=True当前不存在的列名提供给index参数,并设置make_index=True 这将自动创建具有唯一值的列。

例如,在下面的代码中会自动创建event_id索引

import pandas as pd
import featuretools as ft

df = pd.DataFrame({"customer_id": [0, 1, 0, 1, 1],
                   "date": [pd.Timestamp("1/1/2018"), pd.Timestamp("1/1/2018"),
                            pd.Timestamp("1/1/2018"), pd.Timestamp("1/2/2018"),
                            pd.Timestamp("1/2/2018")],
                   "event_type": ["view", "purchase", "view", "cancel", "purchase"]})

es = ft.EntitySet(id="customer_events")                
es.entity_from_dataframe(entity_id="events",
                         dataframe=df,
                         index="event_id",
                         make_index=True,
                         time_index="date")

print(es["events"])

在事件实体中,您可以看到event_id现在是一个变量,即使它不在原始数据框中也是如此

Entity: events
  Variables:
    event_id (dtype: index)
    date (dtype: datetime_time_index)
    customer_id (dtype: numeric)
    event_type (dtype: categorical)
  Shape:
    (Rows: 5, Columns: 4)
  相关解决方案