How I can I calculate a cost per lead/enquire using two different dataframes?

Every quarter I need to calculate the cost per lead/enquire (CPE) from real estate portals. From a complete table that has all the enquiries in the quarter, I need to filter first all enquiries that came from a sub-origin “portals” (specific column), then I need to count the ocurrencies for each portal (including the portals that had 0 enquiries), and then I need to divide each portal´s number of enquiries by the money spent in it to find its CPE (in a new created column).

I have been using Google Colab.

First I used this to filter data from enquiries table and then count portal ocurrencies, turning it into a dataframe:

enquiries_q1_24_portals = enquiries_q1_2024[(enquiries_q1_2024[‘Sub-origin’] == ‘Portal’)]
enquiries_q1_24_portals.groupby([‘Origin’]).size().sort_values(ascending=False)
enquiries_q1_24_portals = pd.DataFrame({‘Enquiries’: enquiries_q1_24_portals})
enquiries_q1_24_portals = pd.to_numeric(enquiries_q1_24_portals[‘Enquiries’], errors=’coerce’)

Then I used loc to separate the two necessary columns from dataframe “portal_costs” which has the spent values:

portal_costs = portal_costs.loc[:, [‘Origin’, ‘CostQ124’]]

And then I merged the two dataframes:

cost_per_enquire = pd.merge(enquiries_q1_24_portals, portal_costs, on = “Origin”, how = “inner”)

It worked well although it is still missing the portals with “0” enquiries:

Final dataframe with enquiries and costs

Then I used this to divide the columns “CostQ124” and “Enquiries”:

cost_per_enquire[‘CPE’] = cost_per_enquire[‘CostQ124’]/cost_per_enquire[‘Enquiries’]

My expectation was to see a final table with “Origin”, “Enquiries”, “CostQ124” and a new column “CPE”, but it returned the error below. Could anybody help me, it seems I am almost there but something is still missing…

Of course, if there is a simpler way of doing this I am totally open to it.

Thank you so much!


TypeError Traceback (most recent call last)
/usr/local/lib/python3.10/dist-packages/pandas/core/ops/array_ops.py in _na_arithmetic_op(left, right, op, is_cmp)
170 try:
–> 171 result = func(left, right)
172 except TypeError:

10 frames
/usr/local/lib/python3.10/dist-packages/pandas/core/computation/expressions.py in evaluate(op, a, b, use_numexpr)
238 # error: “None” not callable
–> 239 return _evaluate(op, op_str, a, b) # type: ignore[misc]
240 return _evaluate_standard(op, op_str, a, b)

/usr/local/lib/python3.10/dist-packages/pandas/core/computation/expressions.py in _evaluate_numexpr(op, op_str, a, b)
127 if result is None:
–> 128 result = _evaluate_standard(op, op_str, a, b)
129

/usr/local/lib/python3.10/dist-packages/pandas/core/computation/expressions.py in _evaluate_standard(op, op_str, a, b)
69 _store_test_result(False)
—> 70 return op(a, b)
71

TypeError: unsupported operand type(s) for /: ‘str’ and ‘int’

During handling of the above exception, another exception occurred:

TypeError Traceback (most recent call last)
in <cell line: 27>()
25 cost_per_enquire = pd.merge(enquiries_q1_24_portals, portal_costs, on = “Origin”, how = “inner”)
26
—> 27 cost_per_enquire[‘CPE’] = cost_per_enquire[‘CostQ124’]/cost_per_enquire[‘Enquiries’]
28
29 display (cost_per_enquire)

/usr/local/lib/python3.10/dist-packages/pandas/core/ops/common.py in new_method(self, other)
79 other = item_from_zerodim(other)
80
—> 81 return method(self, other)
82
83 return new_method

/usr/local/lib/python3.10/dist-packages/pandas/core/arraylike.py in truediv(self, other)
208 @unpack_zerodim_and_defer(“truediv“)
209 def truediv(self, other):
–> 210 return self._arith_method(other, operator.truediv)
211
212 @unpack_zerodim_and_defer(“rtruediv“)

/usr/local/lib/python3.10/dist-packages/pandas/core/series.py in _arith_method(self, other, op)
6110 def _arith_method(self, other, op):
6111 self, other = ops.align_method_SERIES(self, other)
-> 6112 return base.IndexOpsMixin._arith_method(self, other, op)
6113
6114

/usr/local/lib/python3.10/dist-packages/pandas/core/base.py in _arith_method(self, other, op)
1346
1347 with np.errstate(all=”ignore”):
-> 1348 result = ops.arithmetic_op(lvalues, rvalues, op)
1349
1350 return self._construct_result(result, name=res_name)

/usr/local/lib/python3.10/dist-packages/pandas/core/ops/array_ops.py in arithmetic_op(left, right, op)
230 # error: Argument 1 to “_na_arithmetic_op” has incompatible type
231 # “Union[ExtensionArray, ndarray[Any, Any]]”; expected “ndarray[Any, Any]”
–> 232 res_values = _na_arithmetic_op(left, right, op) # type: ignore[arg-type]
233
234 return res_values

/usr/local/lib/python3.10/dist-packages/pandas/core/ops/array_ops.py in _na_arithmetic_op(left, right, op, is_cmp)
176 # Don’t do this for comparisons, as that will handle complex numbers
177 # incorrectly, see GH#32047
–> 178 result = _masked_arith_op(left, right, op)
179 else:
180 raise

/usr/local/lib/python3.10/dist-packages/pandas/core/ops/array_ops.py in _masked_arith_op(x, y, op)
114 # See GH#5284, GH#5035, GH#19448 for historical reference
115 if mask.any():
–> 116 result[mask] = op(xrav[mask], yrav[mask])
117
118 else:

TypeError: unsupported operand type(s) for /: ‘str’ and ‘int’

New contributor

Rafael Fontes is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật