代码之家  ›  专栏  ›  技术社区  ›  Georg Heiler

Featuretools与非唯一联接键的关系

  •  0
  • Georg Heiler  · 技术社区  · 7 年前

    customer_id 以及从网站clickstream事件记录的事件表,其中包含字段 客户id , date

    在尝试创建 https://docs.featuretools.com/loading_data/using_entitysets.html

    Index is not unique on dataframe (Entity transactions)
    

    我怎样才能使它独特或工作?

    1 回复  |  直到 7 年前
        1
  •  1
  •   Max Kanter    7 年前

    如果表中没有可以用作唯一索引的列,可以让featuretools自动创建一个。打电话的时候 EntitySet.entity_from_dataframe(...) 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"])
    

    在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)