我正在处理一个数据框,该数据框包含一列,其数字格式为:
[[45, 45, 'D'],[46, 49, 'C'],[50, 66, 'S'],[67, 101, 'C'],[102, 103, 'S'],[104, 106, 'C'],[107, 108, 'S'],[109, 120, 'C'],[121, 121, 'S'],[122, 123, 'C'],[124, 140, 'S'],[141, 149, 'C'],[150, 176, 'S'],[177, 178, 'C'],[179, 181, 'S'],[182, 194, 'C'],[195, 213, 'S'],[214, 21``7, 'C']]
这些数字与字符串中字符的位置相对应:即字符串:
'MGILSFLPVLATESDWADCKSPQPWGHMLLWTAVLFLAPVAGTPAAPPKAVLKLEPQWINVLQEDSVTLTCRGTHSPESDSIQWFHNGNLIPTHTQPSYRFKANNNDSGEYTCQTGQTSLSDPVHLTVLSEWLVLQTPHLEFQEGETIVLRCHSWKDKPLVKVTFFQNGKSKKFSRSDPNFSIPQANHSHSGDYHCTGNIGYTLYSSKPVTITVQAPSSSPMGIIVAVVTGIAVAAIVAAVVALIYCRKKRISALPGYPECREMGETLPEKPANPTNPDEADKVGAENTITYSLLMHPDALEEPDDQNRI'
如您所见,列表中的某些字符与数字列表中的数字不对应(例如,缺少0-44)。因此,必须删除第0-44位的字符以创建较短的字母序列。
我能够对一行执行此操作,但是我正在努力对数据帧中的每一行执行此操作。
这是用于一行的代码:
new_s = ''
for item in res:
new_s += strSeq[item[0]-1:item[1]]
print(len(new_s), new_s)
这就是我一直试图在所有行中获取的内容:
shortenedSeq_list =[]
counter=0
stringstring=[]
for rows in df.itertuples():
strSeq2 = [rows.sequence]
strremove2 = [rows.shortened_mobidb_consensus]
for item in strremove2:
res = ast.literal_eval(item)
for item in res:
stringstring.append(strSeq2[item[0]-1:item[1]])
stringstring
但这会导致输出:
[],
[],
[],
[],
[],
[],
[],
[],
[],
['MGKGKPRGLNSARKLRVHRRNNRWAETTYKKRLLGTAFKSSPFGGSSHAKGIVLEKIGIESKQPNSAIRKCVRVQLIKNGKKVTAFVPNDGCLNFVDENDEVLLAGFGRKGKAKGDIPGVRFKVVKVSGVSLLALWKEKKEKPRS'],
[],
[],
鉴于此,我希望列表中的每一行都是缩短的序列。
我最终希望将此列表添加为数据框中的一列。
更新
数字是作为字符串而不是列表输出的,因此res是作为列表的数字,这是工作代码输出:
173 AAPPKAVLKLEPQWINVLQEDSVTLTCRGTHSPESDSIQWFHNGNLIPTHTQPSYRFKANNNDSGEYTCQTGQTSLSDPVHLTVLSEWLVLQTPHLEFQEGETIVLRCHSWKDKPLVKVTFFQNGKSKKFSRSDPNFSIPQANHSHSGDYHCTGNIGYTLYSSKPVTITVQAP
,其中173是缩短序列的长度,后跟序列。
df示例:
shortened_mobidb_consensus;sequence
[[45, 45, 'D'], [46, 49, 'C'], [50, 66, 'S'], [67, 101, 'C'], [102, 103, 'S'], [104, 106, 'C'], [107, 108, 'S'], [109, 120, 'C'], [121, 121, 'S'], [122, 123, 'C'], [124, 140, 'S'], [141, 149, 'C'], [150, 176, 'S'], [177, 178, 'C'], [179, 181, 'S'], [182, 194, 'C'], [195, 213, 'S'], [214, 217, 'C']];MGILSFLPVLATESDWADCKSPQPWGHMLLWTAVLFLAPVAGTPAAPPKAVLKLEPQWINVLQEDSVTLTCRGTHSPESDSIQWFHNGNLIPTHTQPSYRFKANNNDSGEYTCQTGQTSLSDPVHLTVLSEWLVLQTPHLEFQEGETIVLRCHSWKDKPLVKVTFFQNGKSKKFSRSDPNFSIPQANHSHSGDYHCTGNIGYTLYSSKPVTITVQAPSSSPMGIIVAVVTGIAVAAIVAAVVALIYCRKKRISALPGYPECREMGETLPEKPANPTNPDEADKVGAENTITYSLLMHPDALEEPDDQNRI
[[1, 1, 'D'], [2, 143, 'S'], [144, 145, 'C']];MGKGKPRGLNSARKLRVHRRNNRWAETTYKKRLLGTAFKSSPFGGSSHAKGIVLEKIGIESKQPNSAIRKCVRVQLIKNGKKVTAFVPNDGCLNFVDENDEVLLAGFGRKGKAKGDIPGVRFKVVKVSGVSLLALWKEKKEKPRS
[[1, 145, 'S']];MGKGKPRGLNSARKLRVHRRNNRWAETTYKKRLLGTAFKSSPFGGSSHAKGIVLEKIGIESKQPNSAIRKCVRVQLIKNGKKVTAFVPNDGCLNFVDENDEVLLAGFGRKGKAKGDIPGVRFKVVKVSGVSLLALWKEKKEKPRS
[[1, 1, 'D'], [2, 2, 'C'], [3, 37, 'S'], [38, 39, 'C'], [40, 40, 'S'], [41, 41, 'C'], [42, 62, 'S'], [63, 65, 'C'], [66, 231, 'S']];MSKNILVLGGSGALGAEVVKFFKSKSWNTISIDFRENPNADHSFTIKDSGEEEIKSVIEKINSKSIKVDTFVCAAGGWSGGNASSDEFLKSVKGMIDMNLYSAFASAHIGAKLLNQGGLFVLTGASAALNRTSGMIAYGATKAATHHIIKDLASENGGLPAGSTSLGILPVTLDTPTNRKYMSDANFDDWTPLSEVAEKLFEWSTNSDSRPTNGSLVKFETKSKVTTWTNL
[[24, 29, 'D'], [30, 91, 'S'], [92, 92, 'D']];MKVSTTALAVLLCTMTLCNQVFSAPYGADTPTACCFSYSRKIPRQFIVDYFETSSLCSQPGVIFLTKRNRQICADSKETWVQEYITDLELNA
答案 0 :(得分:1)
df = pd.read_csv('stringsample.txt',sep=';',converters={0:ast.literal_eval})
for index, row in df.iterrows():
new_s = ''
res = row.shortened_mobidb_consensus
for item in res:
new_s += row.sequence[item[0]-1:item[1]]
df.loc[index,'output'] = new_s
df['output']
0 AAPPKAVLKLEPQWINVLQEDSVTLTCRGTHSPESDSIQWFHNGNL...
1 MGKGKPRGLNSARKLRVHRRNNRWAETTYKKRLLGTAFKSSPFGGS...
2 MGKGKPRGLNSARKLRVHRRNNRWAETTYKKRLLGTAFKSSPFGGS...
3 MSKNILVLGGSGALGAEVVKFFKSKSWNTISIDFRENPNADHSFTI...
4 APYGADTPTACCFSYSRKIPRQFIVDYFETSSLCSQPGVIFLTKRN...
Name: output, dtype: object
df = pd.read_csv('stringsample.txt',sep=';')
shortenedSeq_list =[]
counter=0
stringstring=[]
for rows in df.itertuples():
strSeq2 = rows.sequence
strremove2 = rows.shortened_mobidb_consensus
res = ast.literal_eval(strremove2)
new_s = ''
for item in res:
new_s += strSeq2[item[0]-1:item[1]]
stringstring.append(new_s)
stringstring
['AAPPKAVLKLEPQWINVLQEDSVTLTCRGTHSPESDSIQWFHNGNLIPTHTQPSYRFKANNNDSGEYTCQTGQTSLSDPVHLTVLSEWLVLQTPHLEFQEGETIVLRCHSWKDKPLVKVTFFQNGKSKKFSRSDPNFSIPQANHSHSGDYHCTGNIGYTLYSSKPVTITVQAP',
'MGKGKPRGLNSARKLRVHRRNNRWAETTYKKRLLGTAFKSSPFGGSSHAKGIVLEKIGIESKQPNSAIRKCVRVQLIKNGKKVTAFVPNDGCLNFVDENDEVLLAGFGRKGKAKGDIPGVRFKVVKVSGVSLLALWKEKKEKPRS',
'MGKGKPRGLNSARKLRVHRRNNRWAETTYKKRLLGTAFKSSPFGGSSHAKGIVLEKIGIESKQPNSAIRKCVRVQLIKNGKKVTAFVPNDGCLNFVDENDEVLLAGFGRKGKAKGDIPGVRFKVVKVSGVSLLALWKEKKEKPRS',
'MSKNILVLGGSGALGAEVVKFFKSKSWNTISIDFRENPNADHSFTIKDSGEEEIKSVIEKINSKSIKVDTFVCAAGGWSGGNASSDEFLKSVKGMIDMNLYSAFASAHIGAKLLNQGGLFVLTGASAALNRTSGMIAYGATKAATHHIIKDLASENGGLPAGSTSLGILPVTLDTPTNRKYMSDANFDDWTPLSEVAEKLFEWSTNSDSRPTNGSLVKFETKSKVTTWTNL',
'APYGADTPTACCFSYSRKIPRQFIVDYFETSSLCSQPGVIFLTKRNRQICADSKETWVQEYITDLELNA']