user17432696
user17432696

Reputation: 31

Merge duplicate index and columns in pandas

i want to merge the duplicate index and column values in a dataframe and to not to drop there value, but to merge them to. For example: df

  |A|A|B|B|C|D|
|A|0|1|0|0|0|0|
|A|1|0|0|0|0|0|
|B|0|0|0|1|0|1|
|B|0|0|1|0|0|0|
|C|0|0|0|0|1|0|
|D|0|0|1|0|0|1|

will be like df1

  |A|B|C|D|
|A|1|0|0|0|
|B|0|1|0|0|
|C|0|0|1|0|
|D|0|1|0|1|

does anyone has any idea

Upvotes: 1

Views: 703

Answers (1)

user17242583
user17242583

Reputation:

You can do two groupby's — one by the columns (axis=1), and one by the index, and get the max of each:

>>> df
   A  A  B  B  C  D
A  0  1  0  0  0  0
A  1  0  0  0  0  0
B  0  0  0  1  0  1
B  0  0  1  0  0  0
C  0  0  0  0  1  0
D  0  0  1  0  0  1

>>> df.groupby(df.columns, axis=1).max().groupby(df.index).max()
   A  B  C  D
A  1  0  0  0
B  0  1  0  1
C  0  0  1  0
D  0  1  0  1

Upvotes: 3

Related Questions