0

Lets say I have a variable that I want to export to CSV that looked like this:

matrix([['y', 'x1', 'x2', 'x1^2', 'x2^2', 'x1 * x2', 'x1 * y', 'x2*y'],
        ['38.0', '0.2', '40.0', '0.04000000000000001', '1600.0', '8.0',
         '7.6000000000000005', '1520.0'],
        ['42.0', '0.4', '40.0', '0.16000000000000003', '1600.0', '16.0',
         '16.8', '1680.0'],
        ['41.0', '0.2', '50.0', '0.04000000000000001', '2500.0', '10.0',
         '8.200000000000001', '2050.0'],
        ['46.0', '0.4', '50.0', '0.16000000000000003', '2500.0', '20.0',
         '18.400000000000002', '2300.0'],
        ['46.0', '0.2', '60.0', '0.04000000000000001', '3600.0', '12.0',
         '9.200000000000001', '2760.0'],
        ['49.0', '0.4', '60.0', '0.16000000000000003', '3600.0', '24.0',
         '19.6', '2940.0'],
        ['262.0', '1.8000000000000003', '300.0', '0.6000000000000001',
         '15400.0', '90.0', '79.80000000000001', '13250.0']], dtype='<U32')

I used:

DF = pd.DataFrame(table)
DF.to_csv("data1.csv") 

And the CSV Exported looked like this : enter image description here

Any help so the row contents are not stacked on just one cell?

Tomer Shetah
  • 8,413
  • 7
  • 27
  • 35
  • 1
    I think it is only problem if open in excel, output is correct if check in some text editor, if need excel use `DF.to_excel("data1.xlsx")` instead `DF.to_csv("data1.csv")` – jezrael Mar 03 '21 at 06:58
  • open `csv file` with a spreadsheet software which can recognize the format. – Ferris Mar 03 '21 at 07:01

1 Answers1

0

If you want to convert a numpy array as a csv file, check this answer out - https://stackoverflow.com/a/6081043/13373138

I tried running the below code and it works as expected.

m = ([['y', 'x1', 'x2', 'x1^2', 'x2^2', 'x1 * x2', 'x1 * y', 'x2*y'],
     ['38.0', '0.2', '40.0', '0.04000000000000001', '1600.0', '8.0',
      '7.6000000000000005', '1520.0'],
     ['42.0', '0.4', '40.0', '0.16000000000000003', '1600.0', '16.0',
      '16.8', '1680.0'],
     ['41.0', '0.2', '50.0', '0.04000000000000001', '2500.0', '10.0',
      '8.200000000000001', '2050.0'],
     ['46.0', '0.4', '50.0', '0.16000000000000003', '2500.0', '20.0',
      '18.400000000000002', '2300.0'],
     ['46.0', '0.2', '60.0', '0.04000000000000001', '3600.0', '12.0',
      '9.200000000000001', '2760.0'],
     ['49.0', '0.4', '60.0', '0.16000000000000003', '3600.0', '24.0',
      '19.6', '2940.0'],
     ['262.0', '1.8000000000000003', '300.0', '0.6000000000000001',
      '15400.0', '90.0', '79.80000000000001', '13250.0']])

DF = pd.DataFrame(m)
DF.to_csv("data1.csv")