[Solved] How to generate a “triangular” data frame with as many columns as the row indicates?


Try this:

(df.join(pd.DataFrame(
    df['number']
    .map(lambda x: range(1,x+1)).tolist())
    .rename(lambda x: 'C{}'.format(x+1),axis=1)))

Output:

   number  C1   C2   C3   C4   C5   C6
0       1   1  NaN  NaN  NaN  NaN  NaN
1       2   1  2.0  NaN  NaN  NaN  NaN
2       3   1  2.0  3.0  NaN  NaN  NaN
3       4   1  2.0  3.0  4.0  NaN  NaN
4       6   1  2.0  3.0  4.0  5.0  6.0

0

solved How to generate a “triangular” data frame with as many columns as the row indicates?