python - Calculate during merge using pandas -
is there way perform calculations between columns while merging pandas dataframes? example, have following 2 dfs, , want multiply data corresponding ids:
df1 = dataframe({'id':['a','b','c'], 'data':[8,7,12]}) df2 = dataframe({'id':['b','a','c'], 'data':[3,2,1]})
the desired output be:
id data 0 16 1 b 21 2 c 12
i know possible merging, , multiplying so:
merged = pd.merge(df1, df2, how='left', on=['id']) merged['data'] = merged.data_x * merged.data_y merged[['id', 'data']]
however, large numbers of columns, quite cumbersome, since merged column names have _x
, _y
suffixes. pandas equivalent of following sql:
select a.id, a.data * b.data 'data' table left join table b on a.id = b.id
in case, set index , multiply:
>>> df1 = pd.dataframe({'id':['a','b','c'], 'data':[8,7,12]}) >>> df2 = pd.dataframe({'id':['b','a','c'], 'data':[3,2,1]}) >>> df1.set_index("id") * df2.set_index("id") data id 16 b 21 c 12
with reset_index
if like:
>>> (df1.set_index("id") * df2.set_index("id")).reset_index() id data 0 16 1 b 21 2 c 12
Comments
Post a Comment