0
votes

There is pandas Dataframe as:

number      Num_1   Num_2   Num_3   col_1   col_2   col_3   col_4
915         14      5       8       1       0       1       15:46:14.826872
915         14      5       8       1       1       1                       
915         14      5       8       1       1       0                           
787         12      6       8       1       0       1       15:46:36.168584 
787         12      6       8       1       0       0

Num_1,Num_2,Num_3 - are string values and are same for rows with same "number" col_1,col_2,col_3 - are boolean values. col_4 - string value

How to make some "grouping" for that result:

number      Num_1   Num_2   Num_3   col_1   col_2   col_3   col_4
915         14      5       8       1       1       1       15:46:14.826872
787         12      6       8       1               1       15:46:36.168584 

For col_1,col_2,col_3 i need boolean "OR"

1
Hi, please make the question clearer and produce the example code you used. Also, look at fillna because grouping on empty fields may fail. - 576i

1 Answers

1
votes

Since col_1, col_2, col_3 are just 0s and 1s, you can use max agg function to achieve the OR condition:

df.groupby(['number', 'Num_1', 'Num_2', 'Num_3'], as_index=False).max()

   number  Num_1  Num_2  Num_3  col_1  col_2  col_3            col_4
0     787     12      6      8      1      0      1  15:46:36.168584
1     915     14      5      8      1      1      1  15:46:14.826872

Or:

df.groupby(['number', 'Num_1', 'Num_2', 'Num_3'], as_index=False).agg('max')