通过采样其他列的位来创建新列

问题描述 投票:5回答:1

考虑包含N列的数据帧,如下所示。每个条目都是一个8位整数。

|---------------------|------------------|---------------------|
|      Column 1       |     Column 2     |      Column N       |
|---------------------|------------------|---------------------|
|          4          |         8        |          13         |
|---------------------|------------------|---------------------|
|          0          |         32       |          16         |
|---------------------|------------------|---------------------|

我想通过从剩余列中随机抽取每一位数据,在每一行中创建一个包含8位条目的新列。因此,结果数据框看起来像:

|---------------------|------------------|---------------------|---------------|
|      Column 1       |     Column 2     |      Column N       |     Sampled   |
|---------------------|------------------|---------------------|---------------|
|      4 = (100)      |     8 = (1000)   |    13 = (1101)      |   5 = (0101)  |
|---------------------|------------------|---------------------|---------------|
|      0 = (0)        |    32 = (100000) |   16 = (10000)      | 48 = (110000) |
|---------------------|------------------|---------------------|---------------|

“采样”列中的第一个条目是通过从相同位置的所有可能位中选择一个位来创建的。例如,第一个条目中的LSB = 1是从{0 (LSB from col 1), 0 (LSB from col 2), 1 (LSB from col N)}中选择的,依此类推。

这类似于this question,但不是每个条目都被采样,每个比特都需要进行采样。

考虑到数据帧有大量的行和列,有什么方法可以实现这一目标?从类似的问题,我假设我们需要一个lookup + sample来选择条目和另一个sample来选择位?

python pandas
1个回答
2
投票

像你之前做样本时的逻辑一样,但是在这里我用二进制和十进制之间的转换两次,用unnesting,然后加入结果

df1=df.applymap(lambda x : list('{0:08b}'.format(x)))

df1=unnesting(df1,df1.columns.tolist())
s=np.random.randint(0, df1.shape[1], df1.shape[0])

yourcol=pd.Series(df1.values[np.arange(len(df1)),s]).groupby(df1.index).apply(''.join)

df['Sampled']=yourcol.map(lambda x : int(x,2))

df
Out[268]: 
   c1  c2  cn  Sampled
0   4   8  13       12
1   0  32  16       16

def unnesting(df, explode):
    idx = df.index.repeat(df[explode[0]].str.len())
    df1 = pd.concat([
        pd.DataFrame({x: np.concatenate(df[x].values)}) for x in explode], axis=1)
    df1.index = idx

    return df1.join(df.drop(explode, 1), how='left')
© www.soinside.com 2019 - 2024. All rights reserved.