title: How to Create a New Dataframe From Old Dataframe Based on Conditions
body: <p>I have a situation at work where I need to create a new Dataframe from a previous one</p>
  <p>The previous dataframe looks like:
  |id|name|balance|deleted|minimum_balance|payment_provider_account_id|type|api_user_id|customer_id|
  |-----|------------|------:|------:|--------------:|---------------------------|--------|-|-|
  |38049|LIDIA CIUBUC|3.58   |-1     |2.0            |lidia.ciubuc                      |PERSONAL|NaN|38049|</p>
  <p>The new dataframe will have the same columns, but I need to:
  Check if the name of every single row on previous dataframe already exists on new dataframe
  If it the name does not exist, I add the row on new dataframe
  If it exists, I need to check if deleted status on previous one is -1
  If is not -1, I need to sum the balance of previous dataframe on new datafarme
  If is -1, I need to temporary store the existing balance on new dataframe, replace the row by the one from previous dataframe, and them sum the temporary balance on the replaced row on new dataframe.</p>
  <p>My code so far is:</p>
  <pre><code>import sys, psycopg2
  import pandas as pd
  
  conn = psycopg2.connect(host=&quot;&lt;db_IP&gt;&quot;,port=&lt;DB_port&gt;,dbname=&quot;dbname&quot;,user=&quot;postgres&quot;,password=&quot;Start123&quot;)
  cur = conn.cursor()
  df = pd.read_sql('SELECT * FROM money_account;',conn)
  
  cur.rowcount
  conn.commit()
  cur.close()
  conn.close()
  
  dc = 1
  rc = 0
  print(df.loc[0])
  rows = df.loc[dc-1]
  print(rows)
  type(rows)
  
  for x in rows.id:
      for y in df.id:
          if rows.name[rc] == df.names[dc]:
              if df.deleted[dc] != -1:
                  rows.balance[rc] = rows.balance[rc] + df.balance[dc]
              else:
                  temp = rows.balance[rc]
                  rows[rc] = df[dc]
                  rows.balance[rc] = rows.balance[rc] + temp
          else:
              dc = dc +1
      rc = rc + 1
  print(rows)
  </code></pre>
  <p>But I am receiving the following error:</p>
  <hr />
  <pre><code>TypeError                                 Traceback (most recent call last)
  &lt;ipython-input-4-316da58d0cb6&gt; in &lt;module&gt;
        6 type(rows)
        7 
  ----&gt; 8 for x in rows.id:
        9     for y in df.id:
       10         if rows.name[rc] == df.balance[dc]:
  
  TypeError: 'numpy.int64' object is not iterable
  </code></pre>