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

Add the row counts as a list to column using groupby

  • Thread starter Thread starter Anup
  • Start date Start date
A

Anup

Guest
I am working on an application that needs to provide the count of certain entries in a dataframe. Am missing something that its not rendering the required outcome. Please help.

Input:

Code:
| Release   | Mapping | Coding |
|-----------|---------|--------|
| release_a | A1      | C2     |
| release_c | A1      | C2     |
| release_a | A1      | C2     |
| release_a | A1      | C1     |
| release_b | B       | C1     |
| release_c | B       | C2     |
| release_c | B       | C3     |
| release_a | C       | C1     |
| release_c | A1      | C1     |
| release_c | A1      | C3     |
| release_a | C       | C1     |

Outcome expected:

Code:
| Release   | Mapping      |
|-----------|--------------|
| release_a | A1 - 3, C-2  |
| release_b | B-1          |
| release_c | A1 -3, B - 2 |

Code used:

Code:
df.groupby(['Release', 'Mapping'])['Coding'].agg(count='count')

What i am getting:

enter image description here

May be i havent got a thorough understanding to use agg method. If there is any better alternative also, please suggest. Thanks
<p>I am working on an application that needs to provide the count of certain entries in a dataframe. Am missing something that its not rendering the required outcome. Please help.</p>
<p>Input:</p>
<pre><code>| Release | Mapping | Coding |
|-----------|---------|--------|
| release_a | A1 | C2 |
| release_c | A1 | C2 |
| release_a | A1 | C2 |
| release_a | A1 | C1 |
| release_b | B | C1 |
| release_c | B | C2 |
| release_c | B | C3 |
| release_a | C | C1 |
| release_c | A1 | C1 |
| release_c | A1 | C3 |
| release_a | C | C1 |
</code></pre>
<p>Outcome expected:</p>
<pre><code>| Release | Mapping |
|-----------|--------------|
| release_a | A1 - 3, C-2 |
| release_b | B-1 |
| release_c | A1 -3, B - 2 |
</code></pre>
<p>Code used:</p>
<pre><code>df.groupby(['Release', 'Mapping'])['Coding'].agg(count='count')
</code></pre>
<p>What i am getting:</p>
<p><a href="https://i.sstatic.net/fwH8bc6t.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/fwH8bc6t.png" alt="enter image description here" /></a></p>
<p>May be i havent got a thorough understanding to use agg method. If there is any better alternative also, please suggest. Thanks</p>
 

Latest posts

А
Replies
0
Views
1
Али-Мухаммад Закарьяев
А
M
Replies
0
Views
1
Marcos R. Guevara
M
M
Replies
0
Views
1
Marcos R. Guevara
M
Top