Home > Software design >  how to split array without separator comma in python and fit to row csv
how to split array without separator comma in python and fit to row csv

Time:07-23

i had an output array without sepator comma only seperate with space, when i convert to pandas dataframe the array not split to any row, just stuck in 1 row

the output off arayy looks like this

pred_ct = [217.769 228.838 238.459 225.317 196.812 221.241 214.605 205.918 206.278
 216.028 234.919 224.371 248.084 227.354 231.194 206.726 215.848 238.627
 239.545 246.615 231.238 263.073 249.877 222.093 231.18  242.29  253.832
 211.224 222.574 232.379 218.966 189.899 214.793 208.07  199.198 199.549
 209.464 228.733 218.001 242.169 220.975 224.912 199.978 209.294 232.528
 233.393 240.55  224.901 257.376 243.908 215.563 224.834 236.128 247.95
 204.479 216.109 226.098 212.414 182.785 208.144 201.334 192.279 192.62
 202.701 222.347 211.431 236.054 214.395 218.429 193.03  202.54  226.229
 227.04  234.284 218.365 251.478 237.74  208.833 218.287 229.766 241.869]

pred_ra = [0.54  0.992 1.043 0.718 0.478 0.946 0.678 0.642 0.507 0.499 0.717 0.681
 1.056 0.527 0.969 0.476 0.65  1.01  0.676 0.99  0.652 1.064 0.705 0.463
 0.48  0.502 1.025 0.528 0.984 1.035 0.709 0.465 0.937 0.668 0.631 0.494
 0.487 0.707 0.671 1.049 0.516 0.96  0.463 0.639 1.002 0.666 0.981 0.641
 1.057 0.696 0.45  0.468 0.491 1.017 0.516 0.976 1.028 0.698 0.452 0.928
 0.657 0.619 0.482 0.474 0.697 0.66  1.041 0.504 0.951 0.449 0.628 0.994
 0.655 0.972 0.629 1.049 0.686 0.437 0.456 0.479 1.009]

and i use thisanother code and had no different

import pandas as pd
import numpy as np

datapredct0 = np.array(pred_ct)
datapredct1 = {'CUTTING TEMPERATURE' : [datapredct0]}
datapredct2 = pd.DataFrame(datapredct1, columns=['CUTTING TEMPERATURE'])

datapredra0 = np.array(pred_ra)
datapredra1 = {'SURFACE ROUGHNESS' : [datapredra0]}
datapredra2 = pd.DataFrame(datapredra1, columns=['SURFACE ROUGHNESS'])

alldatapred = pd.concat([datapredct2, datapredra2], axis=1, join='inner')

alldatapred.to_csv('alldata.csv', 
            sep=",",  # char used as separator between columns
            index=False, # do not save index column
            header=True  # do save headers
        )

i not understand to fix the diferent sepator, if i delete comma to this

sep=" "

it make other coloumn merge in one coloumn like

for the preview csv file like this

its split coloumn but the data not split to another row

CodePudding user response:

Don't focus so much on the comma separator. Pay more attention to what kinds of objects you produce. Things like [], are display indicators.

Lets make a simple 1d array:

In [2]: x = np.arange(10)
In [3]: x
Out[3]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [4]: print(x)
[0 1 2 3 4 5 6 7 8 9]

It can be display 2 different ways, the repr and str (as with all python classes). The missing commas in the last distinguish this from a Python list. Commas or not is a display feature.

Calling array on it doesn't change anything:

In [5]: np.array(x)
Out[5]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])

If I make a dataframe with it:

In [6]: df = pd.DataFrame(x, columns=['foo'])
In [7]: df
Out[7]: 
   foo
0    0
1    1
2    2
3    3
4    4
5    5
6    6
7    7
8    8
9    9

But you make a frame via a dict:

In [9]: df = pd.DataFrame({'foo':[x]}, columns=['foo'])
In [10]: df
Out[10]: 
                              foo
0  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

This is a 1 row, 1 column frame, with a numpy array in the only cell. In the Pandas display style, it is hard to tell whether this cell is an array, a list, or a string.

If we write this array to csv, we get:

In [16]: df.to_csv('foo.txt',sep=",",index=False,header=True)
In [17]: cat foo.txt
foo
[0 1 2 3 4 5 6 7 8 9]

csv is a 2d format, so it can't display the array cell value as a comma separated column. Instead it just writes the str(x) format, as in my [4]. The sep parameter applies to columns of the frame, not to arrays within individual columns.

If I load that file back into a dataframe, I get something that looks a lot like the df, except for the commas:

In [18]: df1 = pd.read_csv('foo.txt')
In [19]: df1
Out[19]: 
                     foo
0  [0 1 2 3 4 5 6 7 8 9]

The cell is a string, not a numpy array:

In [21]: df['foo'][0]
Out[21]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
In [22]: df1['foo'][0]
Out[22]: '[0 1 2 3 4 5 6 7 8 9]'

Putting arrays in cells of a dataframe creates a frame that is hard to write as csv, and harder to reload.

  • Related