Home > Back-end >  How to export heterogenous arrays to csv
How to export heterogenous arrays to csv

Time:09-15

Basically what im trying to do is export np.arrays to a .csv file where my data can be stored neatly. I've tried doing this code, but it doesn't work as i would like to.

import numpy as np
import pandas as pd

T11= np.array([1, 2, 3])
T21= np.array([4, 5, 6])
T31= np.array([7, 8, 9, 10])
T41= np.array([11, 12, 13, 14])
T51= np.array([15])

Tx=[T11,T21,T31,T41,T51]

Tx1=pd.DataFrame.transpose(Tx)



df2 = pd.DataFrame(np.array(Tx1),columns=['a', 'b', 'c', 'e', 'f'])
df.to_csv('file.csv', index=False, header=False)

The code gives me this errors, but I don't know how to fix them

'list' object has no attribute 'dtypes'

ValueError: Shape of passed values is (5, 1), indices imply (5, 5)

I'd really like my output to be something like:

a b c d e
1 4 7 11 15
2 5 8 12
3 6 9 13
10 14

CodePudding user response:

Try this:

df = pd.DataFrame([T11,T21,T31,T41,T51], index=['a', 'b', 'c', 'e', 'f']).T
df.to_csv('file.csv', index=False, header=True)
  • Related