3

consider the pd.DataFrame df

df = pd.DataFrame([
        [1, 2, 3, 4, 5],
        [5, 1, 2, 3, 4],
        [4, 5, 1, 2, 3],
        [3, 4, 5, 1, 2],
        [2, 3, 4, 5, 1]
    ], list('abcde'), list('ABCDE'))

How do I align the diagonal values into columns?

I'd like this as the result

enter image description here


i've done this

pd.DataFrame([np.roll(row, -k) for k, (_, row) in enumerate(df.iterrows())],
             df.index, df.columns)

I'm hoping for something more straight forward.

smci
  • 29,564
  • 18
  • 109
  • 144
piRSquared
  • 265,629
  • 48
  • 427
  • 571

2 Answers2

4

You can use numpy solution - for shift is used reversed Series same length as DataFrame (if DataFrame has non numeric and non monotonic index it works nice also):

A = df.values
r = pd.Series(range(len(df)))[::-1] + 1

rows, column_indices = np.ogrid[:A.shape[0], :A.shape[1]]

r[r < 0] += A.shape[1]
column_indices = column_indices - r[:,np.newaxis]

result = A[rows, column_indices]
print (pd.DataFrame(result, df.index, df.columns))
   A  B  C  D  E
a  1  2  3  4  5
b  1  2  3  4  5
c  1  2  3  4  5
d  1  2  3  4  5
e  1  2  3  4  5
Community
  • 1
  • 1
jezrael
  • 729,927
  • 78
  • 1,141
  • 1,090
2

Here's another approach using NumPy broadcasting -

a = df.values
n = a.shape[1]
r = np.arange(n)
col = np.mod(r[:,None] + r,n)
df_out = pd.DataFrame(a[np.arange(n)[:,None],col],columns=df.columns)
Divakar
  • 212,295
  • 18
  • 231
  • 332