Home > OS >  How to provide column titles in Python
How to provide column titles in Python

Time:06-20

I am saving arrays A, B, C, D in four separate columns in a CSV file. However, I don't know how to add column titles. The current and desired outputs are attached.

import numpy as np
import csv 

A=np.array([[1,2,3],[4,5,6],[7,8,9]])
B=np.array([[11,12,13],[14,15,16],[17,18,19]])
C=np.array([[21,22,23],[24,25,26],[27,28,29]])
D=np.array([[31,32,33],[34,35,36],[37,38,39]])

with open('Data.csv', 'w') as f:
        print(A)      
        print(B)  #for pressure
        print(C)  #for velocity
        print(D)
        writer = csv.writer(f)
        writer.writerows(zip([A],[B],[C],[D]))

The current output is

enter image description here

  • Related