How to divide two DataFrames by key
dataframe, pandas, python
Solution
you can use pandas dataframe merging:
merged = pd.merge(A,B,on="key")
answer = merged['val_x']/merged['val_y']
now `answer` is a series with the values you wanted and you can create a new dataframe:
df = pd.DataFrame(zip(merged['key'],answer))
#print df
0 eggs 10
1 ham 10
2 spam 10
#left out header because I used 0 and 1, not key and value, but you get the point, it works!
Problem
Here is a pandas problem. Given two dataframes, for example like this: ``` A = key val ----------- spam 10 eggs 20 ham 30 B = key val ----------- eggs 2 spam 1 ham 3 ``` Note that the rows are permuted. How can I divide `A.val / B.val` so the result is: ``` key val ----------- spam 10 eggs 10 ham 10 ``` In words, the values are divided if their keys match. Order of rows does not matter.