I have a array in python as:
newarray=['Title',
'Salary USD',
'Equity %',
'Equity USD',
'Work location',
'Years of Experience',
'Years at Startup',
'Stage',
'Size',
'Staff electrical engineer',
'$226,000',
'0.002%',
'$650,000',
'San Francisco',
'8.0',
'3.0',
'Series H',
'1001-5000 employees',
'Sales development representative',
'$95,000',
'0.0%',
'-',
'Remote',
'1.0',
'1.0',
'Series H',
'1001-5000 employees',
'Product manager',
'$286,000',
'0.002%',
'$1,460,000',
'Remote US',
'10.0',
'1.0',
'Series H',
'1001-5000 employees',
'Data analytics manager',
'$190,000',
'0.01%',
'$126,000',
'Remote',
'6.0',
'4.0',
'Series H',
'201-500 employees']
There are thousands of data in this newarray. I have only illustrated some.After count of every 9
elements ,I want them in subarray.I just want split the inner data and put it into subarray
. My expected output is:
newarray=[['Title',
'Salary USD',
'Equity %',
'Equity USD',
'Work location',
'Years of Experience',
'Years at Startup',
'Stage',
'Size'],
['Staff electrical engineer',
'$226,000',
'0.002%',
'$650,000',
'San Francisco',
'8.0',
'3.0',
'Series H',
'1001-5000 employees'],
['Sales development representative',
'$95,000',
'0.0%',
'-',
'Remote',
'1.0',
'1.0',
'Series H',
'1001-5000 employees'],
['Product manager',
'$286,000',
'0.002%',
'$1,460,000',
'Remote US',
'10.0',
'1.0',
'Series H',
'1001-5000 employees'],
['Data analytics manager',
'$190,000',
'0.01%',
'$126,000',
'Remote',
'6.0',
'4.0',
'Series H',
'201-500 employees']]
I just want to make subarray of each 9 items.
I tried using splitting using from index 0 to 8 but it didn't worked:
import numpy as np
arr = np.array(newarray)
newarray= np.array_split(newarray, 8)
print(newarray)
CodePudding user response:
IIUC, you want a single 2D array? Then reshape
:
out = arr.reshape((-1,9))
NB. be careful, reshape
requires that you have a multiple of the dimensions, here you need n*9 items in the initial array.
output:
array([['Title', 'Salary USD', 'Equity %', 'Equity USD', 'Work location',
'Years of Experience', 'Years at Startup', 'Stage', 'Size'],
['Staff electrical engineer', '$226,000', '0.002%', '$650,000',
'San Francisco', '8.0', '3.0', 'Series H', '1001-5000 employees'],
['Sales development representative', '$95,000', '0.0%', '-',
'Remote', '1.0', '1.0', 'Series H', '1001-5000 employees'],
['Product manager', '$286,000', '0.002%', '$1,460,000',
'Remote US', '10.0', '1.0', 'Series H', '1001-5000 employees'],
['Data analytics manager', '$190,000', '0.01%', '$126,000',
'Remote', '6.0', '4.0', 'Series H', '201-500 employees']],
dtype='<U32')
CodePudding user response:
res = [newarray[x:x 9] for x in range(0, len(newarray), 9)]