Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Concatenate Pandas Dataframe on rows based on group and column value

I have a Pandas DataFrame like this:

df = pd.DataFrame({'GROUP':['A1', 'A1', 'B2', 'B2', 'B2', 'C3', 'C3'], 'X':[1, -1, -1, 1, -1, -1, 1], 'Y': [-1, 0, 1, -1, -1, -1, -1], 'Z': [-1, -1, -1, -1, 0, 1, -1]})
df
      GROUP     X     Y     Z
0        A1     1    -1    -1
1        A1    -1     0    -1   
2        B2    -1     1    -1
3        B2     1    -1    -1
4        B2    -1    -1     0  
5        C3    -1    -1     1
6        C3     1    -1    -1

I want to conditionally concatenate the df for each groupby GROUP, such that it concatenates the value in the column to either 1 or 0 (both same priority) if they are there for the group, else to -1.

The expected result:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

      GROUP     X     Y     Z
0        A1     1     0    -1
1        B2     1     1     0
2        C3     1    -1     1

>Solution :

If there is always only one 0 or 1 per groups is possible aggregate max:

out = df.groupby('GROUP', as_index=False).max()
print (out)
  GROUP  X  Y  Z
0    A1  1  0 -1
1    B2  1  1  0
2    C3  1 -1  1

If both 0 and 1 per group and 0 has higher priority, you can use:

df = pd.DataFrame({'GROUP':['A1', 'A1', 'B2', 'B2', 'B2', 'C3', 'C3'],
                   'X':[1, 0, -1, 1, -1, -1, 1],
                   'Y': [-1, 0, 1, -1, -1, -1, -1], 
                   'Z': [-1, -1, -1, -1, 0, 1, -1]})
print (df)
  GROUP  X  Y  Z
0    A1  1 -1 -1
1    A1  0  0 -1
2    B2 -1  1 -1
3    B2  1 -1 -1
4    B2 -1 -1  0
5    C3 -1 -1  1
6    C3  1 -1 -1

df1 = df.groupby('GROUP').max().mask(df.groupby('GROUP').min().eq(0), 0).reset_index()
print (df1)
  GROUP  X  Y  Z
0    A1  0  0 -1
1    B2  1  1  0
2    C3  1 -1  1
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading