Answers.Body: <p>I am pretty sure there would be more elegant, in-place solutions but here is a quick solution to your problem. Should be OK if your <code>sequence</code> dictionary is not way too large.</p>
  <pre><code>seq_names = zip(data['old_name'], data['new_name']) # brings together old and new names in tuples
  renamed_sequence = {}
  for i in seq_names:
      renamed_sequence[i[1]] = sequence[i[0]]
  renamed_sequence
  </code></pre>
  <p>EDIT: The section below is not quite accurate, please see the comments below.</p>
  <p>And regarding your comment:</p>
  <blockquote>
  <p>I tried to replace the old names with a for loop, but it was imposible
  to do it.</p>
  </blockquote>
  <p>You are not supposed to loop over and at the same time modify an element at the same time anyway (there might be side effects that you would not think of), hence a brand new dictionary in my code.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-21T11:28:43.327Z
Answers.Text: *"You are not supposed to loop over and at the same time modify an element at the same time anyway"*: really? Why? Is that a Python thing?
Answers.UserDisplayName: terdon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-21T11:42:55.413Z
Answers.Text: @terdon, you are right, I should have been more explicit. For example changing the elements of a list while iterating over it would be fine, on the other hand, adding/removing elements while looping through a list would cause problems. In this particular case, the OP would be modifying the elements of a dictionary rather than changing its size, so an in-place solution using a for loop would be perfectly fine.
Answers.UserDisplayName: haci
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-21T11:44:37.973Z
Answers.Text: Ah, right. Things like entering an infinite loop with something like `list.append(foo)` running in a `for i in list`. Gotcha, thanks.
Answers.UserDisplayName: terdon
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-22T02:23:53.410Z
Answers.Text: I would encourage you to improve your variable naming to something more descriptive: replace `i` with the tuple `(old_name, new_name)`; `names_tuple` can become `old2new` or similar and `new_dict` could be called `renamed`. This yields the in my opinion much more readable assignment `renamed[new_name] = sequence[old_name]` which describes the code's intention. I think this is important because answers here serve as a reference for other coders and we should try to improve the overall quality of scientific software.
Answers.UserDisplayName: Jan Wilamowski
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-11-19T12:34:33.557Z
Answers.LastActivityDate: 2021-11-22T06:40:22.617Z
Answers.LastEditDate: 2021-11-22T06:40:22.617Z
Answers.UserDisplayName: haci
Body: <p>I have into a dictionary some sequences. I would like to replace the sequences name for new names contained into a dataframe.</p>
  <pre><code>#my sequences into a dictionary
  sequence = {&quot;seq01&quot;: &quot;agatcatggctaactatgcacgtgca&quot;,             
             &quot;seq02&quot;: &quot;atatcatggctaactatgcacgtgca&quot;,
             &quot;seq03&quot;: &quot;tgatcatggctaactatgcacgtgca&quot;}
  
  #my data frame containing old names and new names
  data = {&quot;old_name&quot;: [&quot;seq01&quot;, &quot;seq02&quot;,&quot;seq03&quot;], &quot;new_name&quot;: [&quot;sample1&quot;, &quot;sample2&quot;, &quot;sample3&quot;]}
  data = pd.DataFrame(data)
  print(data)
  </code></pre>
  <p>I tried to replace the old names with a for loop, but it was imposible to do it.</p>
  <p>Thank you</p>
  
Tags: <fasta><python>
Title: How do you replace multiple sequence names with new names using python?
ThreadText: # How do you replace multiple sequence names with new names using python?
  
  (Asked by: username_0 on 2021-11-19)
  
  I have into a dictionary some sequences. I would like to replace the sequences name for new names contained into a dataframe.
  
  ```
  #my sequences into a dictionary
  sequence = {"seq01": "agatcatggctaactatgcacgtgca",             
             "seq02": "atatcatggctaactatgcacgtgca",
             "seq03": "tgatcatggctaactatgcacgtgca"}
  
  #my data frame containing old names and new names
  data = {"old_name": ["seq01", "seq02","seq03"], "new_name": ["sample1", "sample2", "sample3"]}
  data = pd.DataFrame(data)
  print(data)
  
  ```
  
  I tried to replace the old names with a for loop, but it was imposible to do it.
  
  Thank you
  
  **Tags:** fasta, python
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_1 on 2021-11-19. Score: 4
  
  I am pretty sure there would be more elegant, in-place solutions but here is a quick solution to your problem. Should be OK if your `sequence` dictionary is not way too large.
  
  ```
  seq_names = zip(data['old_name'], data['new_name']) # brings together old and new names in tuples
  renamed_sequence = {}
  for i in seq_names:
      renamed_sequence[i[1]] = sequence[i[0]]
  renamed_sequence
  
  ```
  
  EDIT: The section below is not quite accurate, please see the comments below.
  
  And regarding your comment:
  
  > I tried to replace the old names with a for loop, but it was imposible
  > to do it.
  
  You are not supposed to loop over and at the same time modify an element at the same time anyway (there might be side effects that you would not think of), hence a brand new dictionary in my code.
  
  > **Comments:**
  > **username_2**: *"You are not supposed to loop over and at the same time modify an element at the same time anyway"*: really? Why? Is that a Python thing?  
  > **username_1**: @username_2, you are right, I should have been more explicit. For example changing the elements of a list while iterating over it would be fine, on the other hand, adding/removing elements while looping through a list would cause problems. In this particular case, the OP would be modifying the elements of a dictionary rather than changing its size, so an in-place solution using a for loop would be perfectly fine.  
  > **username_2**: Ah, right. Things like entering an infinite loop with something like `list.append(foo)` running in a `for i in list`. Gotcha, thanks.  
  > **username_3**: I would encourage you to improve your variable naming to something more descriptive: replace `i` with the tuple `(old_name, new_name)`; `names_tuple` can become `old2new` or similar and `new_dict` could be called `renamed`. This yields the in my opinion much more readable assignment `renamed[new_name] = sequence[old_name]` which describes the code's intention. I think this is important because answers here serve as a reference for other coders and we should try to improve the overall quality of scientific software.  
  
  ---
  
  