2

I am new to python and want to change the data from 1-D array to N-D array. Please see my sample data below:

a = array([['T-Shirts', '1 Piece'],
           ['Capris', 'Leggings', 'Skirts']], dtype=object)

and I want to get the data like:

array([[['T-Shirts'], ['1 Piece']],
       [['Capris'], ['Leggings'], ['Skirts']]], dtype=object)
Joe
  • 10,698
  • 7
  • 47
  • 63
Ryan Ma
  • 29
  • 1
  • 2
    missing some quotation marks? – Laurence Apr 19 '17 at 04:00
  • Still missing quotation marks on the first block of code (`Skirts'`). A suggestion is to organize your data and make sure it looks in good shape as a Python list before you load it into a Numpy array. – jberrio Apr 19 '17 at 04:04
  • I added the quotation marks as suggested by original poster's comment on this answer: http://stackoverflow.com/a/43486172. – Joe Apr 19 '17 at 04:16

3 Answers3

2

Numpy does not support jagged arrays, which is what you are trying to get as your output. Your best bet is to move your data to native Python lists.

a = [['T-Shirts,1 Piece'], ['Capris,Leggings,Skirts']]
out = [[[y] for y in x[0].split(',')] for x in a]
out

# returns:
[[['T-Shirts'], ['1 Piece']], [['Capris'], ['Leggings'], ['Skirts']]]
James
  • 25,833
  • 3
  • 34
  • 55
  • Hi James, thanks for your reply. for my output, I want ['T-Shirts', '1 Piece'] to be separated as ['T-Shirts'], ['1 Piece'] – Ryan Ma Apr 19 '17 at 04:10
1

It's easier using Pandas:

import pandas as pd
# note: removed useless sublists
x = ['T-Shirts,1 Piece', 'Capris,Leggings,Skirts']
pd.Series(x).str.split(',', expand=True)

That gives:

          0         1       2
0  T-Shirts   1 Piece    None
1    Capris  Leggings  Skirts
John Zwinck
  • 207,363
  • 31
  • 261
  • 371
0

Set up

It looks like you're using the numpy package because your code has array(...) and dtype=... formatting. So, to prepare to show code examples, I did these commands:

import numpy as np
a = np.array([['T-Shirts', '1 Piece'], ['Capris', 'Leggings', 'Skirts']])

After those, when I enter:

a

I get this result (your starting point):

array([['T-Shirts', '1 Piece'], ['Capris', 'Leggings', 'Skirts']], dtype=object)

Desired output

When I do this command:

[[[x] for x in l] for l in a]

I get this result:

[[['T-Shirts'], ['1 Piece']], [['Capris'], ['Leggings'], ['Skirts']]]

The command I ran was a Python list comprehension. It is syntactic sugar for making a list with for loops. You can read more here or by searching for "python list comprehension".

Note: I did not use numpy for that conversion. If you want the same kind of list you had before, you could place the code inside np.array( ), like this:

np.array([[[x] for x in l] for l in a])

The result of that command is:

array([[['T-Shirts'], ['1 Piece']], [['Capris'], ['Leggings'], ['Skirts']]], dtype=object)

Bonus

Also, on a side note, you can do this:

[[x] for l in a for x in l]

and get this:

[['T-Shirts'], ['1 Piece'], ['Capris'], ['Leggings'], ['Skirts']]
Community
  • 1
  • 1
Joe
  • 10,698
  • 7
  • 47
  • 63
  • @JohnZwinck he's using Numpy, I believe, hence the formatting of his array. – Joe Apr 19 '17 at 04:17
  • Hi Joseph, this is exactly what I want. Thanks so much! would you please explain more on [[x] for l in a for x in l] – Ryan Ma Apr 19 '17 at 06:14
  • @RyanMa I added some links you can use to find more information. – Joe Apr 19 '17 at 07:56