2

Setup

import pandas as pd
from string import ascii_uppercase

df = pd.DataFrame(np.array(list(ascii_uppercase[:25])).reshape(5, 5))

df

   0  1  2  3  4
0  A  B  C  D  E
1  F  G  H  I  J
2  K  L  M  N  O
3  P  Q  R  S  T
4  U  V  W  X  Y

Question
How do I concatenate the strings along the off diagonals?

Expected Result

0        A
1       FB
2      KGC
3     PLHD
4    UQMIE
5     VRNJ
6      WSO
7       XT
8        Y
dtype: object

What I Tried

df.unstack().groupby(sum).sum()

This works fine. But @Zero's answer is far faster.

piRSquared
  • 285,575
  • 57
  • 475
  • 624

1 Answers1

3

You could do

In [1766]: arr = df.values[::-1, :] # or np.flipud(df.values)

In [1767]: N = arr.shape[0]

In [1768]: [''.join(arr.diagonal(i)) for i in range(-N+1, N)]
Out[1768]: ['A', 'FB', 'KGC', 'PLHD', 'UQMIE', 'VRNJ', 'WSO', 'XT', 'Y']

In [1769]: pd.Series([''.join(arr.diagonal(i)) for i in range(-N+1, N)])
Out[1769]:
0        A
1       FB
2      KGC
3     PLHD
4    UQMIE
5     VRNJ
6      WSO
7       XT
8        Y
dtype: object

You may also do arr.diagonal(i).sum() but ''.join is more explicit.

Zero
  • 74,117
  • 18
  • 147
  • 154
  • This is beautiful! Could you do `arr = df.values[::-1, :]` and avoid the vertical flip for every iteration through the comprehension? – piRSquared Oct 06 '17 at 04:44