How do I create header for rows/columns in a matrix?

I have 4 x 4 matrix and I want to make headers for its row and columns.

  1    2    3    4     
1 *    *    *    *
2 *    *    *    *
3 *    *    *    *
4 *    *    *    *   

header for columns: print(' 1 ',' 2 ',' 3 ',' 4 ') This works

header for rows: print('1', '\n', '2','\n', '3', '\n', '4') This doesn't work

How do I do?

728x90

1 Answers How do I create header for rows/columns in a matrix?

Why bother doing these useless print statements, just use super quick and beautiful pandas:

(an example having * as values):

import pandas as pd
df=pd.DataFrame('*',index=range(1,5),columns=range(1,5))

And now:

print(df)

Is:

   1  2  3  4
1  *  *  *  *
2  *  *  *  *
3  *  *  *  *
4  *  *  *  *

If you clicked on the blue pandas text, you we'll learn about pandas, pandas is not only displaying, you can do anything with data, (not the display, the actual data).

and if you really have to do print statement:

print('  1',' 2',' 3',' 4')
print('1 *  *  *  *\n2 *  *  *  *\n3 *  *  *  *\n4 *  *  *  *') 

Output:

  1  2  3  4
1 *  *  *  *
2 *  *  *  *
3 *  *  *  *
4 *  *  *  *

3 months ago