【问题标题】:Reversal of HotEncoding without reducing columns to one在不将列减少到一的情况下反转 HotEncoding
【发布时间】:2022-01-14 14:33:00
【问题描述】:

我有一个将每笔交易显示为一行的数据集。

例如;

Item_1 Item_2 Item_3
NaN 1 1
1 1 NaN

该表有 611 列,1180 行,因此有 611 个项目和 1180 个事务。

我正在寻找进行购物篮分析,因此我需要将所有具有“1”的行更改为项目“名称”

例如...

Item_1 Item_2 Item_3
NaN Item_2 Item_3
Item_1 Item_2 NaN

然后我的目标是删除标题列,并让每行上的每个事务都对齐,没有 NaN

No_header No_header No_header
Item_2 Item_3 NaN
Item_1 Item_2 NaN

【问题讨论】:

    标签: python pandas dataframe one-hot-encoding


    【解决方案1】:

    试试这个:

    items = df.apply(lambda col: col.map({1: col.name})).apply(lambda row: row[~row.isna()].tolist(), axis=1)
    

    输出:

    >>> items
    0    [Item_2, Item_3]
    1    [Item_1, Item_2]
    dtype: object
    
    >>> type(items)
    pandas.core.series.Series
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-18
      • 2014-11-25
      • 2017-09-10
      • 2021-02-23
      • 1970-01-01
      相关资源
      最近更新 更多