[英]Create new column by sampling bits of other columns
考虑包含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)}
,依此类推。
这与此问题类似,但不是每个条目都被采样,而是需要对每个位进行采样。
考虑到数据帧有大量的行和列,有什么方法可以实现这一目标? 从类似的问题,我假设我们需要一个lookup
+ sample
来选择条目和另一个sample
来选择位?
当你做样本时,就像之前的逻辑一样,但是在这里我在二进制和十进制之间转换两次, 使用者去除 ,然后加入结果
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')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.