我需要根据另一个测试,即我的控制,计算测试的敏感性和特异性。为此,我需要合并三个数据帧。
第一个连接是在包含所有情况的列与包含控件测试结果的另一列之间进行连接。(我知道如何做到这一点,但我展示了前面的步骤,让您了解我最后需要做什么)。
第一个数据框架:
data = [['ch1.1234578C>T'], ['ch2.123459G>A'], ['ch3.234569A>T'], ['chX.246890A>G']]
comparison = pd.DataFrame(data, columns = ['All_common_variants_ID'])
comparison
All_common_variants_ID
1 ch1.1234578C>t
2 ch2.123459G>A
3 ch3.234569A>T
4 chX.246890A>G第二个数据框架:
data = [['ch1.1234578C>T'], ['ch2.123459G>A']]
control = pd.DataFrame(data, columns = ['Sample_ID'])
control
Sample_ID
1 ch1.1234578C>T
2 ch2.123459G>A我已经将这两个数据帧与以下代码合并:
comparative = comparison.merge(control[['Sample_ID']],left_on='All_common_variants_ID',right_on='Sample_ID',how='outer').fillna('Real negative')
comparative = comparative.rename(columns={'Sample_ID': 'CONTROL'})
comparativeAll_common_variants_ID CONTROL
1 ch1.1234578C>T ch1.1234578C>T
2 ch2.123459G>A ch2.123459G>A
3 ch3.234569A>T Real negative
4 chX.246890A>G Real negative现在是我遇到问题的地方。
在comparative数据帧的第一列和第二列的条件下,我需要连接第三个数据帧(test)。
这些条件是:
如果测试数据帧的值与第二列中的值匹配,则为"True-positive".
在提供示例之后,这将是预期的结果。
All_common_variants_ID CONTROL Test
1 ch1.1234578C>T ch1.1234578C>T True-positive # ch1.1234578C>T match with the second column
2 ch2.123459G>A ch2.123459G>A False-negative # ch2.123459G>A is not in my test column
3 ch3.234569A>T Real negative False-positive # ch3.234569A>T match with first column but second column is real negative
4 chX.246890A>G Real negative True-negative # chX.246890A>G is not in my test column and is not in the control column.一些评论:
column
发布于 2021-10-23 21:23:36
使用np.select
# Setup test dataframe
data = [['ch1.1234578C>T'], ['ch3.234569A>T']]
test = pd.DataFrame(data, columns=['Test'])
# Build variables to np.select
condlist = [comparative['CONTROL'].isin(test['Test']),
~comparative['CONTROL'].isin(test['Test'])
& comparative['CONTROL'].ne('Real negative'),
comparative['All_common_variants_ID'].isin(test['Test'])
& comparative['CONTROL'].eq('Real negative')]
choicelist = ['True-positive', 'False-negative', 'False-positive']
default = 'True-negative'
# Create new column
comparative['Test'] = np.select(condlist, choicelist, default)输出:
>>> comparative
All_common_variants_ID CONTROL Test
0 ch1.1234578C>T ch1.1234578C>T True-positive
1 ch2.123459G>A ch2.123459G>A False-negative
2 ch3.234569A>T Real negative False-positive
3 chX.246890A>G Real negative True-negativehttps://stackoverflow.com/questions/69691543
复制相似问题