I want to modify a dataframe (here is a fragment of the dataframe):
taxonomy coverage
ko04141,map04141 338.586386
ko00970,map00970 338.586386
ko00970,map00970 338.586386
I am trying to iteratively delete the first word (the one that contains ko) in a column where the words are separated by a coma. I am running the following loop:
for index,row in df51.iterrows():
word_list = row.taxonomy.split(',')
df51.taxonomy = word_list[1]
When I run the loop, instead of obtaining the full table, I obtain this:
taxonomy coverage
map04112 279917.029342
Do you know why doesn't it iteratively assign the second word to each row and instead keeps only the first entry?
Thank you.
The line
df51.taxonomy = word_list[1]
is doing as you wrote it. As you wrote it, it has no idea what row you are currently iterating on.Do you actually just want to drop the first column? For efficiency you wouldn't do that iteratively.
Also it is bad idea to try and modify something you are iterating on.
Thanks for your kind reply, Wayne! I don't need to drop the first column. The first column contains two words separated by a coma. I need to keep the words containing "map". When I iteratively print word_list[1] python prints the list of words that contain 'map' on them. However, as you mentioned, the line needs to know the row where I am iterating on. I have not found a way of making it clear in the code, and I was hoping to get some help. Thanks again for your time!