title: In PYTHON how do I associate values in dataframes to pass them both to a SQL update?
body: <p>Use Case:
  Project team emails the DBAs an excel with a list of hundreds of usernames that need to be updated for a system  in an oracle db. The dba should be able to run a python script that automates the process of importing the data from the excel, connecting to the db and updating the table.</p>
  <p>My Idea:
  Use pandas to import the data from the excel into a dataframe, cx_oracle to connect to the database, a for loop to go through the dataframe and a sql update to make the changes in the database.</p>
  <p>My Problem:
  It kind of works. There is either an issue with my logic or my choice of data structures. The for loop updates the old usernames for each of the new usernames so I end up just changing a ton of the old usernames into the last new username in my dataframe.</p>
  <p>Code:</p>
  <pre><code>import pandas
  import cx_Oracle
  
  df = pandas.read_excel(input(&quot;Please type the name of the excel spreadsheet provided:&quot;))
  
  &quot;&quot;&quot;
  DBA would type in the name of the xlsx provided which would contain two columns for the
  current username and what their new username should be. Would look similar to the info below
  with hundreds of entries.
  
  Usernames to be changed     new usernames
  
  John.Doe                   jdoe
  William.Wallace            wwallace
  Theodore.Roosevelt         troosevelt
  Andrew.Jackson             ajackson
  Thomas.Jefferson           tjefferson
  Alexander.Spotswood        aspotswood
  &quot;&quot;&quot;
  
  old_usernames = df['Usernames to be changed'].unique()
  new_usernames = df['new usernames'].unique()
  
  for old_username in old_usernames:
      dataframe_values = df[df['Usernames to be changed'] == old_username]
  
  for new_username in new_usernames:
      dfv = df[df['new usernames'] == new_username]
  
  
  
  connection = cx_Oracle.connect(&quot;sys&quot;, input(&quot;Please type the database password:&quot;), &quot;localhost:1521/orcl&quot;, mode=cx_Oracle.SYSDBA)
  cursor = connection.cursor()
  for old_username in old_usernames:
      update_username = &quot;UPDATE SYSTEMUSERS SET USERNAME = '&quot; + str(new_username) + &quot;' WHERE USERNAME = '&quot; + str(old_username) + &quot;'&quot;
      cursor.execute(update_username)
      connection.commit()
  cursor.close()
  connection.close()
  
  &quot;&quot;&quot;
  Results of running this code.
  
  What is looked like before running code.
  
  user_first_name  user_last name   usernames
  
  John             Doe              John.Doe
  William          Wallace          William.Wallace
  Theodore         Roosevelt        Theodore.Roosevelt
  Andrew           Jackson          Andrew.Jackson
  Thomas           Jefferson        Thomas.Jefferson
  Alexander        Spotswood        Alexander.Spotswood
  
  
  What is looks like after running the code.
  
  user_first_name  user_last name   usernames
  
  John             Doe              aspotswood
  William          Wallace          aspotswood
  Theodore         Roosevelt        aspotswood
  Andrew           Jackson          aspotswood
  Thomas           Jefferson        aspotswood
  Alexander        Spotswood        aspotswood
  
  
  What I want it to look like.
  
  user_first_name  user_last name   usernames
  
  John             Doe              jdoe
  William          Wallace          wwallace
  Theodore         Roosevelt        troosevelt
  Andrew           Jackson          ajackson
  Thomas           Jefferson        tjefferson
  Alexander        Spotswood        aspotswood
  &quot;&quot;&quot;
  </code></pre>
  <p>Possible Solution: I cannot find out if this is possible but if I could associate the values from the dataframe so that the old username would only be replaced by its specific new username then that would solve all my problems. Otherwise if I could break up my loop somehow so that it would descend through the dataframe in order. My final thought is if I should be using something like an array instead of a dataframe so that I could assign each value in the array to a variable and set some type of counter so that a different loop could go through each of them as a set. I have tried a lot of different ways and have read everything I can find and still cannot make it work.</p>
  <p>Disclaimer:
  I am a noob who has learned 100% of what little he knows from open sources on the internet and I have been researching this issue for a week and am so close, but just can't figure it out. This is the first program I have ever written that interacts with anything outside of itself. If anyone could point me in the right direction then I would greatly appreciate it.</p>
  <p>Additional information:
  OS: Windows 10.0.18363<br />
  Programming Language: Python 3.8.5 Windows x86-64<br />
  Database: Oracle Solaris x86 systems, 64-bit, Oracle SQL Developer 20.2.0.175.1842-x64
  Excel: 16.0.13001.20266 32-bit</p>