熊猫中的自定义虚拟编码

时间:2017-10-24 20:32:20

标签: python pandas dummy-variable

我有一个包含事件数据的数据框。我有两列:主要和次要。主列和辅助列都包含标签列表(例如,[' Fun event',' Dance party'])。

      primary               secondary                      combined
['booze', 'party']    ['singing', 'dance']    ['booze', 'party', 'singing', 'dance']
    ['concert']        ['booze', 'vocals']     ['concert', 'booze', 'vocals']

我想对数据进行伪代码处理,以便主列具有1个代码,非观察列具有0,并且辅助列中的值具有.5值。像这样:

combined                                 booze        party   singing    dance    concert    vocals
['booze', 'party', 'singing', 'dance']     1            1       .5        .5        0           0
['concert', 'booze', 'vocals']            .5            0        0         0        1          .5

2 个答案:

答案 0 :(得分:1)

这是一种通过转换primarysecondary列'来实现的方法。值到数据帧的列中:

df = pd.DataFrame({
        'primary': [['booze', 'party'], ['concert']],
        'secondary': [['singing', 'dance'], ['booze', 'vocals']],
    })

# create primary and secondary indicator columns
iprim = df.primary.apply(lambda v: pd.Series([1] * len(v), index=v))
isec = df.secondary.apply(lambda v: pd.Series([.5] * len(v), index=v))

# join with primary, then update from secondary columns
df = df.join(iprim).join(isec, rsuffix='_')
df.drop([c for c in df.columns if c.endswith('_')], axis=1, inplace=True)
df.update(isec)
df.fillna(0)

=>

    primary        secondary        booze   concert     party      dance    singing     vocals
0   [booze, party] [singing, dance] 1.0     0.0         1.0         0.5         0.5     0.0
1   [concert]      [booze, vocals]  0.5     1.0         0.0         0.0         0.0     0.5

请注意,第二个.join()使用rsuffix添加已在primary中的列,而.update()用于覆盖主列中的值。 .drop()删除了这些列。重新排列以优先选择初级中学。

答案 1 :(得分:1)

df1=pd.get_dummies(df.combined.apply(pd.Series).stack()).sum(level=0)
df1[df1.apply(lambda x : [x.name in y for y in df.iloc[x.index,2]])]-=0.5

df1
Out[173]: 
   booze  concert  dance  party  singing  vocals
0    1.0        0    0.5      1      0.5     0.0
1    0.5        1    0.0      0      0.0     0.5

Datainput:

df = pd.DataFrame({'primary':   [['booze', 'party'] ,  ['concert']],
                   'secondary':   [['singing', 'dance'], ['booze', 'vocals']],
                   'combined': [['booze', 'party', 'singing', 'dance'],   ['concert', 'booze', 'vocals']]})