ekta
ekta

Reputation: 1620

Flatten the results of a group by in a python dataframe after printing the grouped instance counts

I need to count the instances of two columns in a dataframe by values. I get the same by using group & size, though I want to spit out 1. The flat value in each column combination 2. the name of the "last count" column (See also the what I want below).

    df = pd.DataFrame([[1.1, 1.1, 1.1, 2.6, 2.5, 3.4,2.6,2.6,3.4,3.4,2.6,1.1,1.1,3.3], list('AAABBBBABCBDDD'), [1.1, 1.7, 2.5, 2.6, 3.3, 3.8,4.0,4.2,4.3,4.5,4.6,4.7,4.7,4.8], ['x/y/z','x/y','x/y/z/n','x/u','x','x/u/v','x/y/z','x','x/u/v/b','-','x/y','x/y/z','x','x/u/v/w'],['1','3','3','2','4','2','5','3','6','3','5','1','1','1']]).T
    df.columns = ['col1','col2','col3','col4','col5']
    df.groupby(['col5', 'col2']).size()
# this gives 
col5  col2   <Note that this is unnamed>
1     A       1
      D       3
2     B       2
3     A       3
      C       1
4     B       1
5     B       2
6     B       1
dtype: int64

What I want -:
    col5  col2   count_instances_of_this_combination
    1     A       1
    1     D       3
    2     B       2
    3     A       3
    3     C       1
    4     B       1
    5     B       2
    6     B       1

That is I explicitly want the 1st columns to print out the complete combination of col5, col2

Related question : Pandas DataFrame Groupby two columns and get counts

  col1 col2 col3     col4 col5
0   1.1    A  1.1    x/y/z    1
1   1.1    A  1.7      x/y    3
2   1.1    A  2.5  x/y/z/n    3
3   2.6    B  2.6      x/u    2
4   2.5    B  3.3        x    4
5   3.4    B  3.8    x/u/v    2
6   2.6    B    4    x/y/z    5
7   2.6    A  4.2        x    3
8   3.4    B  4.3  x/u/v/b    6
9   3.4    C  4.5        -    3
10  2.6    B  4.6      x/y    5
11  1.1    D  4.7    x/y/z    1
12  1.1    D  4.7        x    1
13  3.3    D  4.8  x/u/v/w    1

This means the combination <1,A > occurred once, <2, B> occurred twice, <1,d> occurred thrice & so on.

Here's how it worked -:

Further to the answer below, on setting the sparity option to False, I did this to get the name .

pd.options.display.multi_sparse = False
# rest same a above.. 
s=pd.DataFrame({'s=pd.DataFrame({'count_instances_of_this_combination' : df.groupby(['query', 'product_id']).size()}).reset_index()' : df.groupby(['col5', 'col2']).size()}).reset_index()

This gives me a well formed data frame with the "3rd" column as a named column.

Upvotes: 2

Views: 2583

Answers (1)

unutbu
unutbu

Reputation: 880299

Set the option:

pd.options.display.multi_sparse = False

Then:

import pandas as pd
pd.options.display.multi_sparse = False

df = pd.DataFrame(
   [[1.1, 1.1, 1.1, 2.6, 2.5, 3.4,2.6,2.6,3.4,3.4,2.6,1.1,1.1,3.3], 
   list('AAABBBBABCBDDD'), 
   [1.1, 1.7, 2.5, 2.6, 3.3, 3.8,4.0,4.2,4.3,4.5,4.6,4.7,4.7,4.8], 
   ['x/y/z','x/y','x/y/z/n','x/u','x','x/u/v','x/y/z','x','x/u/v/b','-','x/y',
      'x/y/z','x','x/u/v/w'],
   ['1','3','3','2','4','2','5','3','6','3','5','1','1','1']]).T

df.columns = ['col1','col2','col3','col4','col5']
print(df.groupby(['col5', 'col2']).size())

yields

col5  col2
1     A       1
1     D       3
2     B       2
3     A       3
3     C       1
4     B       1
5     B       2
6     B       1
dtype: int64

Upvotes: 5

Related Questions