OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

convert lambda function to regular function PYTHON df["domain_count"] = df.apply(lambda row : df['domain'].value_counts()[row['domain']], axis = 1)

  • Thread starter Thread starter codingrainha
  • Start date Start date
C

codingrainha

Guest
I have this current lambda function: df["domain_count"] = df.apply(lambda row : df['domain'].value_counts()[row['domain']], axis = 1)

But I want to convert it to a regular function like this def get_domain_count() how do I do this? I'm not sure what parameters it would take in as I want to apply it to an entire column in a dataframe? The domain column will contain duplicates and I want to know how many times a domain appears in my dataframe.

Code:
ex start df:

|domain|
---
|target.com|
|macys.com|
|target.com|
|walmart.com|
|walmart.com|
|target.com|

ex end df:
|domain|count|
---|---|
|target.com|3
|macys.com|1
|target.com|3
|walmart.com|2
|walmart.com|2
|target.com|3

Please help! Thanks in advance!
<p>I have this current lambda function: <code>df["domain_count"] = df.apply(lambda row : df['domain'].value_counts()[row['domain']], axis = 1)</code></p>
<p>But I want to convert it to a regular function like this <code>def get_domain_count()</code> how do I do this? I'm not sure what parameters it would take in as I want to apply it to an entire column in a dataframe? The <code>domain</code> column will contain duplicates and I want to know how many times a domain appears in my dataframe.</p>
<pre><code>ex start df:

|domain|
---
|target.com|
|macys.com|
|target.com|
|walmart.com|
|walmart.com|
|target.com|

ex end df:
|domain|count|
---|---|
|target.com|3
|macys.com|1
|target.com|3
|walmart.com|2
|walmart.com|2
|target.com|3
</code></pre>
<p>Please help! Thanks in advance!</p>
 
Top