嗨
这里是python的Noobie
我有>;10000个代表肽序列的字符串。字符串中的每个字母都是一个氨基酸,在用预定义的浮点值(从-1到-2)替换每个字母后,我想计算字符串的“净和”
我被困在哪里开始与循环,使这项工作?我有清理字符串的代码,以便删除非字母字符,并用字典中定义的浮点值替换(即W:2.10,G:-1.0)
我想代码是这样的
我有6个数据帧来重复这个过程
任何帮助都将不胜感激
更新的代码(这要感谢SARAH MESSER)
def hydrophobicity_score(peptide):
hydro = {
'A': -0.5,
'C': -1.0,
'D': 3.0,
'E': 3.0,
'F': -2.5,
'G': 0.0,
'H': -0.5,
'I': -1.8,
'K': 3.0,
'L': -1.8,
'M': -1.3,
'N': 0.2,
'P': 0.0,
'Q': 0.2,
'R': 3.0,
'S': 0.3,
'T': -0.4,
'V': -1.5,
'W': -3.4,
'Y': -2.3,
}
hydro_score = [hydro.get(aa,0.0)for aa in peptide]
return sum(hydro_score)
og_pep['Hydro'] = og_pep['Peptide'].apply(hydrophobicity_score)
og_pep
我没有得到预期的输出
Output
Here is og_pep DataFrame
好的,首先,您不想在数据帧中的行上循环。这些行被设计为并行处理。了解这一点有些费劲,但一旦定义了一些行级操作并将其应用于大型数据帧,就会变得更平滑。(行上循环的问题是速度中的一个问题。它有时在调试或玩具问题中很有用,但现代计算硬件试图尽可能地并行计算。数据帧利用这一点一次处理所有行,而不是在循环中单独处理它们。)
要进行转换,您需要定义一个自定义函数来对每一行进行操作。然后将该自定义函数传递给dataframe,并告诉它apply将该行级函数传递给一列,以便生成一个新列
因此,这里有一个可能的函数让您开始:
如果您有很多要忽略的字符(空格、标点符号等),可以将列表中的
amino_acid_scores[character]
替换为amino_acid_scores.get(character, 0.0)
相关问题 更多 >
编程相关推荐