1

Matrix A:

    Count    First    Second
    0.5      1        a
    0.5      1        b
    1.0      1        b
    1.0      2        a 
    0.5      2        a 
    1.0      3        b     

I need to create a new matrix from an original matrix by identifying all rows with matching "First" and "Second" values. Group these rows together and add the values in the "Count" Column. "Matrix B" would be the end result of running the code on "Matrix A":

NEW - Matrix B

    Count    First    Second
    0.5      1        a
    1.5      1        b
    1.5      2        a 
    1.0      3        b  

Likely there is a simple method to perform this action. But I am at a loss right now. I currently working with a large numpy array.

1 Answers1

0

Simple try with groupby

out = df.groupby(['First','Second'], as_index=False)['Count'].sum()
Out[163]: 
   First Second  Count
0      1      a    0.5
1      1      b    1.5
2      2      a    1.5
3      3      b    1.0
BENY
  • 317,841
  • 20
  • 164
  • 234
  • @engee_soup17 if this work for you , would you like accept the answer ? – BENY Sep 07 '20 at 02:27
  • If my data is raw, and none of the columns have any names associated with them (IE: "first" or "second") to group by... is there a generic term that can be applied for specifying the columns to group or is it required that I set some type of name to my column and rows in pandas? – engee_soup17 Sep 07 '20 at 02:45