我有一个csv文件,结构如下:
num mut
36 L
45 P
...
,其中num表示突变的位置,mut表示突变。我必须在位置num上修改字母mut,变成字符串。我用python写了下面的代码:
import pandas as pd
import os
df = pd.read_csv(r'file.csv')
df_tmp=df.astype(str)
df_tmp["folder"]=df_tmp["num"]+df_tmp["mut"] #add a third column
f = open("sequence.txt", 'r')
content = f.read()
for i in range(len(df)):
num=df_tmp.num.loc[[i]]-13
num=num.astype(int)
prev=num-1
prev=prev.astype(int)
mut=df_tmp.mut.loc[[i]]
mut=mut.astype(str)
new="".join((content[:prev],mut,content[num:])) #this should modify the file
但是它返回我
TypeError: slice indices must be integers or None or have an __index__ method
怎么解?
编辑:也许这是更清楚我想做什么。我必须在序列中只插入第一个突变,将其保存到一个文件中,将该文件复制到名为第三列的文件夹中(我在代码中添加的),对第二个突变做同样的事情,然后是第三个,依此类推。但是我每次只能插入一个突变。多重突变:
IIUC,你最好是熊猫,将你的数据框架转换为字典,迭代和连接:
# input DataFrame
df = pd.DataFrame({'num': [36, 45], 'mut': ['L', 'P']})
# input string
string = '-'*50
# '--------------------------------------------------'
# get the positions to modify
pos = df.set_index('num')['mut'].to_dict()
# {36: 'L', 45: 'P'}
# iterate over the string, replace hte characters if in the dictionary
# NB. define start=1 if you want the first position to be 1
new_string = ''.join([pos.get(i, c) for i,c in enumerate(string, start=0)])
# '------------------------------------L--------P----'
单突变:
string = '-'*50
# '--------------------------------------------------'
for idx, r in df.iterrows():
new_string = string[:r['num']-1]+r['mut']+string[r['num']:]
# or
# new_string = ''.join([string[:r['num']-1], r['mut'], string[r['num']:]])
with open(f'file_{idx}.txt', 'w') as f:
f.write(new_string)
输出:
file_0.txt
-----------------------------------L--------------
file_1.txt
--------------------------------------------P-----
我用一个示例文件。csv和一个空的sequence.txt文件尝试了你的代码,
从for循环 开始的第一行中的
num=df_tmp.num.loc[[i]]-13
#gives an error since the num in that location is str, to correct that:
num=df_tmp.num.loc[[i]].astype(int)-13
# I used astype to convert it into int first
在此之后,下一个错误在最后一行,切片索引类型错误,这是由于您用于切片的结果prev和numcontent变量不是int,要获得int值,请向其添加[0]以这种方式:
content="".join((content[:prev[0]],mut,content[num[0]:]))
现在不应该有错误了。