-3

consider the below array (EDIT: the format of the array is because I get this data from another source, basically the data is nested list of lists of unequal elements)

eg = array([
   [ list(['Vehicle Sales Anfavea units','Brazil','184,815.00'])],
   [ list(['Commodity Price Index MoM % m/m', 'Brazil', '-0.58'])],
   [ list(['Commodity Price Index YoY % y/y', 'Brazil', '0.54'])]
 ], dtype=object)

how can i slice this array in a single attempt to get last two elements in each row? my desired output should be something like

([
['Brazil','184,815.00'],
['Brazil', '-0.58'],
['Brazil', ''0.54']
])
6
  • Is you one-element list [list(['Vehicle Sales Anfavea units','Brazil','184,815.00'])] intentional or did you actually mean ['Vehicle Sales Anfavea units','Brazil','184,815.00']? In the latter case, the answer is eg[:,[1,2]]. In the former case, eg[:,0,[1,2]]. Commented Sep 6, 2017 at 20:01
  • This is not a 2D array. You may want to familiarize yourself with the syntax to construct lists and arrays before you go further; the list calls are redundant, and the brackets around the list calls are most likely not doing anything you want. Commented Sep 6, 2017 at 20:09
  • i get the point, but the format of this array is because I am getting the data from another source. Commented Sep 6, 2017 at 20:20
  • Are you saying this is the repr of the array? That's very different from if you had actually executed the code you posted. Commented Sep 6, 2017 at 20:27
  • I'm pretty sure it would have said dtype=object in the repr if it had that explicit list notation, though. Commented Sep 6, 2017 at 20:29

4 Answers 4

0

It looks like the display of a (3,1) object array, containing 3 lists:

In [168]: arr = np.zeros((3,1),object)
In [169]: arr[:,0]=[['Vehicle Sales Anfavea units','Brazil','184,815.00'],['Comm
     ...: odity Price Index MoM % m/m', 'Brazil', '-0.58'],['Commodity Price Ind
     ...: ex YoY % y/y', 'Brazil', '0.54']]
In [170]: arr
Out[170]: 
array([[list(['Vehicle Sales Anfavea units', 'Brazil', '184,815.00'])],
       [list(['Commodity Price Index MoM % m/m', 'Brazil', '-0.58'])],
       [list(['Commodity Price Index YoY % y/y', 'Brazil', '0.54'])]], dtype=object)

A simple copy-n-paste creates a (3,1,3) array objects (strings); not a 2d array of lists.

Since you want a slice of the lists, you'll have use a list comprehension

In [171]: [a[-2:] for a in arr.ravel()]
Out[171]: [['Brazil', '184,815.00'], ['Brazil', '-0.58'], ['Brazil', '0.54']]
In [172]: np.array(_)
Out[172]: 
array([['Brazil', '184,815.00'],
       ['Brazil', '-0.58'],
       ['Brazil', '0.54']],
      dtype='<U10')

Notice that this final array is (3,2) string dtype, not an object array of lists.

Another approach is to turn it into a 2d string array, and index that:

In [174]: np.stack(arr.ravel())
Out[174]: 
array([['Vehicle Sales Anfavea units', 'Brazil', '184,815.00'],
       ['Commodity Price Index MoM % m/m', 'Brazil', '-0.58'],
       ['Commodity Price Index YoY % y/y', 'Brazil', '0.54']],
      dtype='<U31')
In [175]: _.shape
Out[175]: (3, 3)
In [176]: __[:,-2:]
Out[176]: 
array([['Brazil', '184,815.00'],
       ['Brazil', '-0.58'],
       ['Brazil', '0.54']],
      dtype='<U31')

More about this use of stack at How to turn array of array into single high dimension array?


To keep the object array of lists structure, an inplace replacement might be easiest:

In [180]: arr1=arr.copy()
In [181]: arr1.shape
Out[181]: (3, 1)
In [182]: for a in arr1.ravel():
     ...:     a[:] = a[-2:]
     ...:     
In [183]: arr1
Out[183]: 
array([[list(['Brazil', '184,815.00'])],
       [list(['Brazil', '-0.58'])],
       [list(['Brazil', '0.54'])]], dtype=object)
Sign up to request clarification or add additional context in comments.

1 Comment

np.array(arr.tolist()) is a convenient way to turn an object array of lists into a regular array, though I can't say whether it's the most efficient.
0

OK, so taking your data as is,

import numpy as np
eg = np.array([
   [ list(['Vehicle Sales Anfavea units','Brazil','184,815.00'])],
   [ list(['Commodity Price Index MoM % m/m', 'Brazil', '-0.58'])],
   [ list(['Commodity Price Index YoY % y/y', 'Brazil', '0.54'])]
 ], dtype=object)

You can simply run following one liner to get needed values.

x = [ i.tolist() for i in [i[0][1:] for i in eg] ]

[i[0][1:] for i in eg] parses list of list and i.tolist() converts np.array value to a list

>>> 
>>> x
[['Brazil', '184,815.00'], ['Brazil', '-0.58'], ['Brazil', '0.54']]

Comments

0

Here is one way, you'll get some "nesting" though:

In [1]: from numpy import array

In [2]: eg = array([
   ...:    [ list(['Vehicle Sales Anfavea units','Brazil','184,815.00'])],
   ...:    [ list(['Commodity Price Index MoM % m/m', 'Brazil', '-0.58'])],
   ...:    [ list(['Commodity Price Index YoY % y/y', 'Brazil', '0.54'])]
   ...:  ], dtype=object)

In [3]: eg[:,:,-2:]
Out[3]:
array([[['Brazil', '184,815.00']],

       [['Brazil', '-0.58']],

       [['Brazil', '0.54']]], dtype=object)

You can however "squeeze" it:

In [4]: import numpy as np

In [5]: np.squeeze(eg[:,:,-2:])
Out[5]:
array([['Brazil', '184,815.00'],
       ['Brazil', '-0.58'],
       ['Brazil', '0.54']], dtype=object)

2 Comments

You didn't start with the same array; your's is (3,1,3), not (3,2) with lists.
I copy pasted his example.
0

Your array may look like

array([
   [ list(['Vehicle Sales Anfavea units','Brazil','184,815.00'])],
   [ list(['Commodity Price Index MoM % m/m', 'Brazil', '-0.58'])],
   [ list(['Commodity Price Index YoY % y/y', 'Brazil', '0.54'])]
 ], dtype=object)

but running that as code will not reproduce your array, and trying to slice the sublists as if they were part of the array's structure won't work.

You have a 2-dimensional array of object dtype whose elements are lists. Since version 1.13, NumPy will explicitly show list(...) for lists inside an array in the repr view, but NumPy won't see the list(...) notation if you try to execute the repr representation as code, and it will infer the depth of the array from the input, usually not preserving the lists.


You need to eliminate the redundant singleton dimension, convert the array of lists to a normal multidimensional array, and then slice:

eg = eg[:, 0]
eg = numpy.array(eg.tolist())
eg = eg[:, 1:]

Comments

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.