Pandas sum corresponding values based on values in another column
Question:
I have a table, df1, containing columns Itemlist1 and Itemlist2 where each cell in this table can contain any number of items starting from 1.
I have another table, df2, with the Price and Cost of each item.
I want to create this final df with 2 new columns added to df1, Totalprice and Totalcost. The Totalprice and Totalcost is the sum of all the items in each row of df1.
I am thinking of combining all the items into a column in df1, splitting each item into a column and then merging it with df2. As the number of items in each row is not fixed and the columns names also do not match, how can I loop this to merge with df2?
Alternatively, is there a better approach to arrive at the final df I want? Any suggestions please. Thank you.
Answers:
From your df3, do the replace
, then sum
with axis=1
cost_dict = dict(zip(df2.Itemcode,df2.Cost))
price_dict = dict(zip(df2.Itemcode,df2.Price))
df1['totalcost'] = df3.replace(cost_dict).sum(axis=1)
df1['totalprice'] = df3.replace(price_dict).sum(axis=1)
I have a table, df1, containing columns Itemlist1 and Itemlist2 where each cell in this table can contain any number of items starting from 1.
I have another table, df2, with the Price and Cost of each item.
I want to create this final df with 2 new columns added to df1, Totalprice and Totalcost. The Totalprice and Totalcost is the sum of all the items in each row of df1.
I am thinking of combining all the items into a column in df1, splitting each item into a column and then merging it with df2. As the number of items in each row is not fixed and the columns names also do not match, how can I loop this to merge with df2?
Alternatively, is there a better approach to arrive at the final df I want? Any suggestions please. Thank you.
From your df3, do the replace
, then sum
with axis=1
cost_dict = dict(zip(df2.Itemcode,df2.Cost))
price_dict = dict(zip(df2.Itemcode,df2.Price))
df1['totalcost'] = df3.replace(cost_dict).sum(axis=1)
df1['totalprice'] = df3.replace(price_dict).sum(axis=1)