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 - Group by with same dimension

  • Thread starter Thread starter Denis Padua
  • Start date Start date
D

Denis Padua

Guest
I have a dataframe that has five colums: G1, G2, G3, G4 and Tax. I'm grouping by G and Tax for each G column to count how many rows has the value 1. But the dimension of result is different for each G because some G's only have 0 values.

This is my code:

Code:
self.instance.df.groupby(["G1", "Tax"]).size().drop(0)
self.instance.df.groupby(["G2", "Tax"]).size().drop(0)

I'm dropping the index 0 because i only need 1 values.

The result i'm expecting is:

Code:
G1  Tax
1   1.1   5
    1.2   7
    1.5   3
    1.7   0

and the result i'm getting:

Code:
 G1 Tax
 1  1.1   5
    1.2   7
    1.5   3
<p>I have a dataframe that has five colums: G1, G2, G3, G4 and Tax. I'm grouping by G and Tax for each G column to count how many rows has the value 1. But the dimension of result is different for each G because some G's only have 0 values.</p>
<p>This is my code:</p>
<pre><code>self.instance.df.groupby(["G1", "Tax"]).size().drop(0)
self.instance.df.groupby(["G2", "Tax"]).size().drop(0)
</code></pre>
<p>I'm dropping the index 0 because i only need 1 values.</p>
<p>The result i'm expecting is:</p>
<pre><code>G1 Tax
1 1.1 5
1.2 7
1.5 3
1.7 0
</code></pre>
<p>and the result i'm getting:</p>
<pre><code> G1 Tax
1 1.1 5
1.2 7
1.5 3
</code></pre>
 
Top