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)
3
  • 2
    missing some quotation marks? Commented Apr 19, 2017 at 4: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. Commented Apr 19, 2017 at 4:04
  • I added the quotation marks as suggested by original poster's comment on this answer: stackoverflow.com/a/43486172. Commented Apr 19, 2017 at 4:16

3 Answers 3

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']]]
Sign up to request clarification or add additional context in comments.

1 Comment

Hi James, thanks for your reply. for my output, I want ['T-Shirts', '1 Piece'] to be separated as ['T-Shirts'], ['1 Piece']
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

Comments

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']]

3 Comments

@JohnZwinck he's using Numpy, I believe, hence the formatting of his array.
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]
@RyanMa I added some links you can use to find more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.