I have a csv file structured in the following way:
num mut
36 L
45 P
...
where num indicates the position of a mutation and mut indicates the mutation. I have to modify at the position num with the letter mut a string. I wrote the following code in 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
But it returns me
TypeError: slice indices must be integers or None or have an __index__ method
How can I solve?
Edit: maybe it is more clear what I want to do. I have to insert only the first mutation in my sequence, save it to a file, copy the file in a folder that is named as the third column (that I added in the code), make the same thing with the second mutation, then the third and so on. But I have to insert only one mutation at time.
O(n**2)while you can do it inO(n)