【发布时间】:2019-01-25 19:12:51
【问题描述】:
我正在尝试使用填充有如下值的列来转换 pandas 数据框:
df['Alteration']
Q79K,E17K
Q79K,E17K
T315I
并且想将单字母氨基酸转换为其三字母代码,使其看起来更像这样:
Gln79Lys,Glu17Lys
Gln79Lys,Glu17Lys
Thr315Ile
到目前为止,我已经尝试过使用正则表达式作为键的字典,例如:
AA_code = {re.compile('[C]'): 'Cys',re.compile('[D]'): 'Asp',
re.compile('[S]'): 'Ser',re.compile('[Q]'): 'Gln',re.compile('[K]'): 'Lys',
re.compile('[I]'): 'Ile',re.compile('[P]'): 'Pro',re.compile('[T]'): 'Thr',
re.compile('[F]'): 'Phe',re.compile('[N]'): 'Asn',re.compile('[G]'): 'Gly',
re.compile('[H]'): 'His',re.compile('[L]'): 'Leu',re.compile('[R]'): 'Arg',
re.compile('[W]'): 'Trp',re.compile('[A]'): 'Ala',re.compile('[V]'): 'Val',
re.compile('[E]'): 'Glu',re.compile('[Y]'): 'Tyr',re.compile('[M]'): 'Met'}
并根据字典替换以下代码:
df['Replacement'] = dfx2['Alteration'].replace(AA_code, regex=True)
但是,我得到了一些奇怪的行为,替换函数覆盖了值,看起来更像这样:
Glyln79Leuys,Glu17Leuys
Glyln79Leuys,Glu17Leuys
Thr315Ile
据我了解,Glyln 源自代码首先将 Q 更改为 Gln,然后 Gln 中的 G 被 G 覆盖: Gly key : 字典中的值对来获取 Glyln。有什么办法可以解决这个问题吗??
谢谢!!
【问题讨论】:
标签: python regex pandas dictionary