我有一个熊猫df,看起来是这样的:
df = pd.DataFrame({'index': {0: 34, 1: 35, 2: 36, 3: 37, 4: 38},
'lane': {0: 1, 1: 1, 2: 1, 3: 1, 4: 1},
'project': {0: 'default',
1: 'default',
2: 'default',
3: 'default',
4: 'default'},
'sample': {0: 'None-BORD1778',
1: 'None-BORD1779',
2: 'None-BORD1780',
3: 'None-BORD1782',
4: 'None-BORD1783'},
'barcode_sequence': {0: 'AACCTACG',
1: 'TTGCGAGA',
2: 'TTGCTTGG',
3: 'TACACACG',
4: 'TTCGGCTA'},
'pf_clusters': {0: '"1,018,468"',
1: '"750,563"',
2: '"752,191"',
3: '"876,957"',
4: '"695,347"'},
'%_of_the_lane': {0: 0.28, 1: 0.21, 2: 0.21, 3: 0.24, 4: 0.19},
'%_perfect_barcode': {0: 100.0, 1: 100.0, 2: 100.0, 3: 100.0, 4: 100.0},
'yield_(mbases)': {0: '511', 1: '377', 2: '378', 3: '440', 4: '349'},
'%_pf_clusters': {0: 100.0, 1: 100.0, 2: 100.0, 3: 100.0, 4: 100.0},
'%_>=_q30_bases': {0: 89.74, 1: 89.9, 2: 89.0, 3: 89.31, 4: 88.69},
'mean_quality_score': {0: 35.13, 1: 35.15, 2: 34.98, 3: 35.04, 4: 34.92}})
我现在试着做以下几点。对于barcode_sequence
列下的每个值,我想逐个字符比较它们与同一列下所有其他值的相似程度
为此,我定义了以下函数:
def compare(s1,s2):
return len([x for x in range(len(s1)) if s1[x] == s2[x]])/len(s1)
现在我想将这个函数应用于df['barcode_sequence']
下的每个值。这意味着,在我的第一次迭代中(其中s1
是AACCTACG
),我将把函数compare
应用于同一列下的所有其他值,即AACCTACG
与TTGCGAGA
、TTGCTTGG
、TACACACG
和TTCGGCTA
。然后我会对第二行TTGCGAGA
(现在是我的新值s1
)执行相同的操作,依此类推,直到到达df['barcode_sequence']
下的最后一个条目
到目前为止,我已经得到了df['barcode_sequence']
下每个条目所需的迭代次数,这可以通过嵌套for循环与iterrows()
方法的组合来实现。因此,如果我这样做:
for index, row in df.iterrows():
for sample in list(range(len(df.index))):
print(index, row['sample'],row['barcode_sequence'])
我至少可以得到我正在比较的字符串(我的s1
在compare
)以及我将为每个s1
进行的比较的数量
尽管我一直在为每个s1
提取所有的s2
下面是一种使用交叉连接格式的方法(不需要显式for循环):
相关问题 更多 >
编程相关推荐