David Jopo
David Jopo

Reputation: 7

Dataframe or index make symetrical based on the upper triangle

I would like to make the data frame df symmetrical based on the upper numbers. As can be seen in the example in the picture. [Example]

1

Any ideas how could this be done most efficiently?

Upvotes: 0

Views: 29

Answers (1)

bb1
bb1

Reputation: 7863

You can try the following:

import numpy as np

arr = df.to_numpy()
out = pd.DataFrame(np.triu(arr, 0) + np.triu(arr, 1).T)

Upvotes: 1

Related Questions