Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

426
Views
How to build triangular matrix as df with data from dataframe

Let's say: I have dataframe like this:

import pandas as pd

df = pd.DataFrame({'a':['a1','a2','a3','a4','a5'],'b':[1,2,3,4,5]})

Output:

    a  b
0  a1  1
1  a2  2
2  a3  3
3  a4  4
4  a5  5

Now I would like to add new columns corresponding to a1, a2, a3, a4, a5

for index, row in df.iterrows():
    df[index] = np.NaN

Output:

    a  b   0   1   2   3   4
0  a1  1 NaN NaN NaN NaN NaN
1  a2  2 NaN NaN NaN NaN NaN
2  a3  3 NaN NaN NaN NaN NaN
3  a4  4 NaN NaN NaN NaN NaN
4  a5  5 NaN NaN NaN NaN NaN

How can I generate a triangular matrix in the dataframe?

I would like the following output:

    a  b 0   1   2   3   4
0  a1  1 a1  a2  a3  a4  a5
1  a2  2 NaN a2  a3  a4  a5
2  a3  3 NaN NaN a3  a4  a5
3  a4  4 NaN NaN NaN a4  a5
4  a5  5 NaN NaN NaN NaN a5

Should I build one extra dataframe as a triangular matrix and then merge? What is the easiest way?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

This will work:

for i in range(df.shape[0]):
    df.iloc[i, i+2:] = df['a'][i:]

Output:

>>> df
    a  b   0   1   2   3   4
0  a1  1 NaN NaN NaN NaN NaN
1  a2  2 NaN NaN NaN NaN NaN
2  a3  3 NaN NaN NaN NaN NaN
3  a4  4 NaN NaN NaN NaN NaN
4  a5  5 NaN NaN NaN NaN NaN

>>> for i in range(df.shape[0]):
...    df.iloc[i, i+2:] = df['a'][i:]

>>> df
    a  b    0    1    2    3   4
0  a1  1   a1   a2   a3   a4  a5
1  a2  2  NaN   a2   a3   a4  a5
2  a3  3  NaN  NaN   a3   a4  a5
3  a4  4  NaN  NaN  NaN   a4  a5
4  a5  5  NaN  NaN  NaN  NaN  a5
over 4 years ago · Santiago Trujillo Report

0

An efficient approach would be to use numpy (numpy.tile and numpy.triu):

import numpy as np
np.triu(np.tile(df['a'].values, (len(df), 1)))

Then craft a dataframe and join:

df.join(pd.DataFrame(np.triu(np.tile(df['a'].values, (len(df), 1))),
                     index=df.index
                     ).replace({0: pd.NA}))

output:

    a  b     0     1     2     3   4
0  a1  1    a1    a2    a3    a4  a5
1  a2  2  <NA>    a2    a3    a4  a5
2  a3  3  <NA>  <NA>    a3    a4  a5
3  a4  4  <NA>  <NA>  <NA>    a4  a5
4  a5  5  <NA>  <NA>  <NA>  <NA>  a5
alternative
n = len(df)
df.join(pd.DataFrame(np.tile(df['a'].values, (n, 1)), index=df.index
                     ).where(np.triu(np.ones((n, n))).astype(bool)))

output:

    a  b    0    1    2    3   4
0  a1  1   a1   a2   a3   a4  a5
1  a2  2  NaN   a2   a3   a4  a5
2  a3  3  NaN  NaN   a3   a4  a5
3  a4  4  NaN  NaN  NaN   a4  a5
4  a5  5  NaN  NaN  NaN  NaN  a5
over 4 years ago · Santiago Trujillo Report

0

My solution was similar to mozway but not in 3 steps:)

import pandas as pd
import numpy as np

d = { "a":['a1','a2','a3','a4','a5']  }
"create dataframe from dict"

df = pd.DataFrame(d)
indek1 = list(df.index)
column1 = df['a'].values
lista_tot = []
for k in indek1:
    lista_tot.append(column1)

a = np.triu(lista_tot, 0)

df1 = pd.DataFrame(a)
print(df1)

df = df.join(df1)
print(df)
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!