I have a requirement where i have some input data in one df which needs to be compare the 'name' column with other df which has same column, and if the match is found i need to do update those to my table.
Issue: The last row of my data_to_update is applying to all the columns.
What i have tried so far:
import pandas as pd
import numpy as np
import cx_Oracle
conn = cx_Oracle.connect('xxxxx', 'yyyyyy',dsn_tns)
cursor = conn.cursor()
data = [{'name': 'ABC', 'col1': 10, 'col2': 20, 'col3': 'John'},
{'name': 'DEF', 'col1': 30, 'col2': 40, 'col3': 'Peter'},
{'name': 'PQR', 'col1': 50, 'col2': 60, 'col3': 'Mary'},
{'name': 'XYZ', 'col1': 70, 'col2': 80, 'col3': 'Robert'}]
df = pd.DataFrame(data)
data2 = [{'name': 'ABC', 'col1': 10, 'col2': 20000, 'col3': 'XXXX'},
{'name': 'DEF', 'col1': 30, 'col2': 40, 'col3': 'Peter'},
{'name': 'PQR', 'col1': 50, 'col2': 60, 'col3': 'Mary'},
{'name': 'XYZ', 'col1': 70, 'col2': 80000, 'col3': 'YYYY'}]
df2 = pd.DataFrame(data)
df['match'] = np.where(df['name'].isin(df2['name']), 1, 0)
exist_df = df[df['match'] == 1]
del exist_df['match']
new_df = df[df['match'] == 0]
del new_df['match']
update_list = exist_df['name'].tolist()
to_update = "','".join(update_list)
to_update1 = "('" + to_update + "')"
data_to_update = [tuple(x) for x in exist_df[['col2','col3']].values]
update_query = ''' update mytable set col2 =: col2, col3 =: col3 where name in ''' + to_update1
cursor.executemany(update_query,data_to_update)
conn.commit()
My table data before is:
name col1 col2 col3
ABC 10 20 John
DEF 30 40 Peter
PQR 50 60 Mary
XYZ 70 80 Robert
Data after running above code is:
name col1 col2 col3
XYZ 70 80000 YYYY
XYZ 70 80000 YYYY
XYZ 70 80000 YYYY
XYZ 70 80000 YYYY
But the expected table data after the process:
name col1 col2 col3
ABC 10 20000 XXXX
DEF 30 40 Peter
PQR 50 60 Mary
XYZ 70 80000 YYYY
Any help is highly appreciated, thanks in advance!