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

Pandas groupby - group columns to a list of count of row values

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

Anup

Guest
I want to group my dataframe according to a column [Say: Release] with a sum of values from other rows in a customized representation.

My Dataframe:

Code:
|  Release  | Pass | Fail | Total |
|:---------:|:----:|:----:|:-----:|
| release_a | 10   | 20   | 30    |
| release_a | 5    | 45   | 50    |
| release_a | 5    | 23   | 28    |
| release_a | 20   | 67   | 87    |
| release_a | 87   | 11   | 98    |
| release_b | 2    | 5    | 7     |
| release_b | 10   | 45   | 55    |
| release_b | 64   | 33   | 97    |
| release_c | 3    | 15   | 18    |
| release_c | 104  | 89   | 193   |
| release_c | 98   | 87   | 185   |

Output expected:

Code:
|  Release  |         Summary        |
|:---------:|:----------------------:|
| release_a | [Pass: 40, Fail: 55]   |
| release_b | [Pass: 76, Fail: 83]   |
| release_c | [Pass: 205, Fail: 191] |

I tried using groupby and agg, but couldnt achieve my expected result.

I used the below codes:

Code:
df.groupby(['Release Name'],as_index=False).agg({'Pass': lambda x: x.to_numpy().tolist()})

Code:
df['Release Name'].map(df.value_counts('Pass'))
<p>I want to group my dataframe according to a column [Say: Release] with a sum of values from other rows in a customized representation.</p>
<p>My Dataframe:</p>
<pre><code>| Release | Pass | Fail | Total |
|:---------:|:----:|:----:|:-----:|
| release_a | 10 | 20 | 30 |
| release_a | 5 | 45 | 50 |
| release_a | 5 | 23 | 28 |
| release_a | 20 | 67 | 87 |
| release_a | 87 | 11 | 98 |
| release_b | 2 | 5 | 7 |
| release_b | 10 | 45 | 55 |
| release_b | 64 | 33 | 97 |
| release_c | 3 | 15 | 18 |
| release_c | 104 | 89 | 193 |
| release_c | 98 | 87 | 185 |
</code></pre>
<p>Output expected:</p>
<pre><code>| Release | Summary |
|:---------:|:----------------------:|
| release_a | [Pass: 40, Fail: 55] |
| release_b | [Pass: 76, Fail: 83] |
| release_c | [Pass: 205, Fail: 191] |
</code></pre>
<p>I tried using groupby and agg, but couldnt achieve my expected result.</p>
<p>I used the below codes:</p>
<pre><code>df.groupby(['Release Name'],as_index=False).agg({'Pass': lambda x: x.to_numpy().tolist()})
</code></pre>
<pre><code>df['Release Name'].map(df.value_counts('Pass'))
</code></pre>
 

Latest posts

Online statistics

Members online
0
Guests online
3
Total visitors
3
Ads by Eonads
Top