October 22, 2024
Chicago 12, Melborne City, USA
python

Iterate data pandas between two tables


I’m trying to add a new column to my customers table (table). This new column is the sum of pending orders for a product (SKU). Pending orders are in another table (tableOV). I managed to do this, but it is taking a long time to calculate. I would like to know if there is a more efficient way.

table

[table]

tableOV

[tableOV]

table with new pendent sum column

[table with new pendent sum column]

my code

table = pd.DataFrame(productsCustomerCd)

tableOV['CC'] = tableOV['CC'].astype(str)
tableOV['SKU'] = tableOV['SKU'].astype(str)
table['cc'] = table['cc'].astype(str)

sumPendentList = []
for i in table.itertuples():
    sumPendent = tableOV.loc[(tableOV['CC']==i.cc)].loc[(tableOV['SKU']==i.sku), 'Pendent'].sum()
    sumPendentList.append(sumPendent)

    print(i.cc, i.sku, sumPendent)

table['Pendent'] = sumPendentList

print(table)

I’m trying to add a new column to my customers table (table). This new column is the sum of pending orders for a product (SKU). Pending orders are in another table (tableOV). I managed to do this, but it is taking a long time to calculate. I would like to know if there is a more efficient way.



You need to sign in to view this answers

Leave feedback about this

  • Quality
  • Price
  • Service

PROS

+
Add Field

CONS

+
Add Field
Choose Image
Choose Video