У меня есть следующие два (упрощенных) кадра данных:
df1=
origin destination val1 val2
0 1 A 0.8 0.9
1 1 B 0.3 0.5
2 1 c 0.4 0.2
3 2 A 0.4 0.7
4 2 B 0.2 0.1
5 2 c 0.5 0.1
df2=
org price
0 1 50
1 2 45
Мне нужно выбрать цену для каждого источника из df2, умножить ее на сумму val1 + val2 в df1 и запишите его в файл csv.
Расчет для A выглядит следующим образом:
A => (0,8 + 0,9) * 50 + (0,4+ 0,7) * 45 = 134,5
здесь значения 0,8, 0,9, 0,4 и 0,7 происходят от df1, и они соответствуют val1 и val2 для A, где значения 50 и 45 берутся из df2, соответствующего началу 1 и 2 соответственно. для B расчет будет
B => (0,3 + 0,5) * 50 + (0,2 + 0,1) * 45 = 53,5
для C расчет будет:
C => (0,4 + 0,2) * 50 + (0,5 + 0,1) * 45 = 57
Окончательный файл CSV должен выглядеть следующим образом:
A, 134,5
B, 53,5
C, 57 Для этого я написал следующий python код:
# first convert the second table into a python dictionary so that I can refer price value at each origin
df2_dictionary = {}
for ind in df2.index:
df2_dictionary[df2['org'][ind]] = float(df2['price'][ind])
# now go through df1, add up val1 and val2 and add the result to the result dictionary.
result = {}
for ind in df1.index:
origin = df1['origin'][ind]
price = df2_dictionary[origin] # figure out the price from the dictionary.
r = (df1['val1'][ind] + df1['val2'][ind])*price # this is the needed calculation
destination = df1['destination'][ind] # store the result in destination
if(destination in result.keys()):
result[destination] = result[destination]+r
else:
result[destination] = r
f = open("result.csv", "w")
for key in result:
f.write(key+","+str(result[key])+"\n")
f.close()
Это много работы и не использует pandas встроенные функции. Как мне это упростить? Я не так беспокоюсь об эффективности.