如何从数据帧子集中随机选择元素?

时间:2018-06-04 08:47:53

标签: python pandas

我有以下格式的数据框:

W1 W2 W3 W4 0 1 1 0 1 1 1 1 1 0 0 0 0 1 0 1

对于每一行,我想随机选择 1 的单个元素,并使其他元素为零。初始零保持为零(例如

W1 W2 W3 W4 0 1 0 0 0 1 0 0 1 0 0 0 0 0 0 1

我有一个非常复杂的解决方案,使用iterrows(),但我正在寻找一个pandastic的解决方案。

3 个答案:

答案 0 :(得分:2)

构思是提取位置,随机删除,然后按第一列0删除重复项 - 按行:

#get positions of 1
a = np.where(df == 1)

#create nd array
X = np.hstack((a[0][:, None], a[1][:, None]))
#shuffling
np.random.shuffle(X)

#remove duplicates
vals = pd.DataFrame(X).drop_duplicates(0).values

#set 1
arr = np.zeros(df.shape)
arr[vals[:,0],vals[:,1]] = 1

df = pd.DataFrame(arr.astype(int), columns=df.columns, index=df.index)
print (df)
   W1  W2  W3  W4
0   0   0   1   0
1   0   0   0   1
2   1   0   0   0
3   0   1   0   0

答案 1 :(得分:2)

IIUC,你想从每一行中随机选择1并完成剩下的0.这是一种方法。对指数进行抽样并根据指数分配1.即

idx = pd.DataFrame(np.stack(np.where(df==1))).T.groupby(0).apply(lambda x: x.sample(1)).values
# array([[0, 2],
#        [1, 1],
#        [2, 0],
#        [3, 3]])

ndf = pd.DataFrame(np.zeros(df.shape),columns=df.columns)

ndf.values[idx[:,0],idx[:,1]] = 1

   W1  W2  W3  W4
0   0   0   1   0
1   1   0   0   0
2   1   0   0   0
3   0   1   0   0

答案 2 :(得分:1)

这是功能和pandastic方法的混合:

df = pd.DataFrame({'w1': [0, 1,1,0],
                   'w2': [1, 1,0,1],
                   'w3': [1, 1,0,0],
                   'w4': [0, 1,0,1]})
df
   w1  w2  w3  w4
0   0   1   1   0
1   1   1   1   1
2   1   0   0   0
3   0   1   0   1


def choose_one(row):
    """
    returns array with randomly chosen positive value and 0 otherwise
    """
    one = np.random.choice([i for i, v in enumerate(row) if v])
    return [0 if i != one else 1 for i in range(len(row))]

申请每一行

df.apply(choose_one, 1)

   w1  w2  w3  w4
0   0   1   0   0
1   0   1   0   0
2   1   0   0   0
3   0   0   0   1
相关问题