Python根据条件创建ID的组合

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

嗨,我想创建ID的组合。我知道如何创建所有可能的组合,但我坚持操作的最后一部分。任何帮助将不胜感激。

我有一个数据集如下:

从itertools import combination_with_replacement导入pandas as pd

d1 = {'Subject': ['Subject1','Subject1','Subject1','Subject2','Subject2','Subject2','Subject3','Subject3','Subject3','Subject4','Subject4','Subject4','Subject5','Subject5','Subject5'],
'Actual':['1','0','0','0','0','1','0','1','0','0','0','0','1','0','1'],
'Event':['1','2','3','1','2','3','1','2','3','1','2','3','1','2','3'],
'Category':['1','1','2','1','1','2','2','2','2','1','1','1','1','2','1'],
'Variable1':['1','2','3','4','5','6','7','8','9','10','11','12','13','14','15'],
'Variable2':['12','11','10','9','8','7','6','5','4','3','2','1','-1','-2','-3'],
'Variable3': ['-6','-5','-4','-3','-4','-3','-2','-1','0','1','2','3','4','5','6']}
d1 = pd.DataFrame(d1)

我想在每个层次中的每个事件中创建所有可能的主题组合。这是通过(从之前的问题Form groups of individuals python (pandas))完成的:

L = [(i[0], i[1], y[0], y[1]) for i, x in d1.groupby(['Event','Category'])['Subject'] 
                          for y in list(combinations_with_replacement(x, 2))]
df = pd.DataFrame(L, columns=['Event','Category','Subject_IDcol1','Subject_IDcol2'])

现在,我想把所有那些Actual = 1并且随机选择“n”的对,其中Actual = 0.这里为了简单起见,我们假设n = 1.我想在这个新列表上运行combination_with_replacement函数。

我想得到的输出(假设随机选择)是这样的:

对于事件1,类别1:主题1和5具有实际= 1并且假设主题2是随机绘制的。

enter image description here

与此相比,在前一种情况下,结果是这样的(对于事件= 1和类别= 1)

enter image description here

任何帮助将不胜感激。谢谢。

python pandas random data-manipulation
1个回答
1
投票

我认为这是做你想要的一种方式:

import itertools
import pandas as pd
import numpy as np

d1 = {
    'Subject': ['Subject1', 'Subject1', 'Subject1', 'Subject2', 'Subject2', 'Subject2',
                'Subject3', 'Subject3', 'Subject3', 'Subject4', 'Subject4', 'Subject4',
                'Subject5', 'Subject5', 'Subject5'],
    'Actual': ['1', '0', '0', '0', '0', '1', '0', '1', '0', '0', '0', '0', '1', '0', '1'],
    'Event': ['1', '2', '3', '1', '2', '3', '1', '2', '3', '1', '2', '3', '1', '2', '3'],
    'Category': ['1', '1', '2', '1', '1', '2', '2', '2', '2', '1', '1', '1', '1', '2', '1'],
    'Variable1': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15'],
    'Variable2': ['12', '11', '10', '9', '8', '7', '6', '5', '4', '3', '2', '1', '-1', '-2', '-3'],
    'Variable3': ['-6', '-5', '-4', '-3', '-4', '-3', '-2', '-1', '0', '1', '2', '3', '4', '5', '6']
}
d1 = pd.DataFrame(d1)
num_nonactual = 1

np.random.seed(100)
# First leave only up to num_nonactual subjects with actual != '1' for each event/category
g1 = d1.groupby(['Event', 'Category', 'Actual'], group_keys=False)
d2 = g1.apply(lambda x: x if x.name[2] == '1' else x.sample(min(num_nonactual, len(x))))
# Then do the same as before
d2.sort_values('Subject', inplace=True)
L = [(i1, i2, y1, y2)
     for (i1, i2), x in d2.groupby(['Event', 'Category'])['Subject']
     for y1, y2 in itertools.combinations_with_replacement(x, 2)]
df = pd.DataFrame(L, columns=['Event', 'Category', 'Subject_IDcol1', 'Subject_IDcol2'])
print(df)

输出:

   Event Category Subject_IDcol1 Subject_IDcol2
0      1        1       Subject1       Subject1
1      1        1       Subject1       Subject4
2      1        1       Subject1       Subject5
3      1        1       Subject4       Subject4
4      1        1       Subject4       Subject5
5      1        1       Subject5       Subject5
6      1        2       Subject3       Subject3
7      2        1       Subject2       Subject2
8      2        2       Subject3       Subject3
9      2        2       Subject3       Subject5
10     2        2       Subject5       Subject5
11     3        1       Subject4       Subject4
12     3        1       Subject4       Subject5
13     3        1       Subject5       Subject5
14     3        2       Subject2       Subject2
15     3        2       Subject2       Subject3
16     3        2       Subject3       Subject3
© www.soinside.com 2019 - 2024. All rights reserved.