Related
Could somebody please tell me how can I merge the following list:
[[['a', 'b'], ['b', 'a'], ['c', 'c']], [['d', 'e'], ['e', 'd']]]
into:
[['ab', 'ba', 'cc'], ['de', 'ed']]?
Thank you!
IIUC, you need to map the join on all the sublists:
l = [[['a', 'b'], ['b', 'a'], ['c', 'c']], [['d', 'e'], ['e', 'd']]]
out = [list(map(''.join, x)) for x in l]
Or:
out = [[''.join(i) for i in x] for x in l
Output: [['ab', 'ba', 'cc'], ['de', 'ed']]
Two methods:
Use map() and ''.join:
data = [[['a', 'b'], ['b', 'a'], ['c', 'c']], [['d', 'e'], ['e', 'd']]]
result = [list(map(''.join, item)) for item in data]
print(result)
Use a list comprehension:
data = [[['a', 'b'], ['b', 'a'], ['c', 'c']], [['d', 'e'], ['e', 'd']]]
result = [[''.join(item) for item in sublist] for sublist in data]
print(result)
Both of these print:
[['ab', 'ba', 'cc'], ['de', 'ed']]
I have a list with the length of n:
labels = ['a', 'b', 'c', 'd']
and a array with the size of m*n like:
values = array([[0. , 0.6, 0.3, 0.1],
[0.5, 0.1, 0.1, 0.3],
[0.1, 0.2, 0.3, 0.4]])
I wish to sort labels by each line of values and to generate a new m*n array like:
labels_new = [['a', 'd', 'c', 'b'],
['b', 'c', 'd', 'a'],
['a', 'b', 'c', 'd']]
Is there any simple way to achieve this?
You can use argsort function. Just use a numpy array for preserving the labels and then a simple indexing:
In [6]: labels = np.array(['a', 'b', 'c', 'd'])
In [7]: labels[np.argsort(values)]
Out[7]:
array([['a', 'd', 'c', 'b'],
['b', 'c', 'd', 'a'],
['a', 'b', 'c', 'd']], dtype='<U1')
Function argsort arranges an array in a srted order and returns the corresponding array of indexes. That array can be used to reorder the labels:
[array(labels)[np.argsort(v)].tolist() for v in values]
#[['a', 'd', 'c', 'b'],
# ['b', 'c', 'd', 'a'],
# ['a', 'b', 'c', 'd']]
I am trying to sum the values of select columns from a list of columns and store it in a new column. However, I keep getting
raise KeyError('%s not in index' % objarr[mask])
KeyError: "['a' 'b' 'c' 'd' 'e'\n 'f'] not in index"
This is the piece of code where I am trying to achieve this :
cols = df.columns.values
colIndex = np.argwhere('Person')
selectCols = np.delete(cols, colIndex)
df['total counts'] = df[selectCols].sum(axis=1)
First I'm not sure how the \n is present after column e and secondly I don't know what's causing this KeyError. Please help!
In [290]: np.argwhere('Person')
Out[290]: array([], shape=(1, 0), dtype=int64)
It doesn't make sense to use that in np.delete.
Show cols
===
In [301]: cols = np.array(['Person', 'a', 'b', 'c', 'd', 'e', 'f'])
In [302]: idx = np.argwhere('Person')
In [303]: idx
Out[303]: array([], shape=(1, 0), dtype=int64)
In [304]: np.delete(cols, idx)
Out[304]: array(['Person', 'a', 'b', 'c', 'd', 'e', 'f'], dtype='<U6')
alternatively we can find where cols is equal to 'Person':
In [305]: idx = np.argwhere(cols=='Person')
In [306]: idx
Out[306]: array([[0]])
In [307]: np.delete(cols, idx)
Out[307]: array(['a', 'b', 'c', 'd', 'e', 'f'], dtype='<U6')
or working with a list version of cols:
In [313]: alist = cols.tolist()
In [314]: alist
Out[314]: ['Person', 'a', 'b', 'c', 'd', 'e', 'f']
In [315]: alist.remove('Person')
In [316]: alist
Out[316]: ['a', 'b', 'c', 'd', 'e', 'f']
===
replicating more of your case:
In [317]: df = pd.DataFrame(np.ones((2,4),int), columns=['a','b','c','d'])
In [318]: df
Out[318]:
a b c d
0 1 1 1 1
1 1 1 1 1
In [319]: cols = df.columns.values
In [320]: cols
Out[320]: array(['a', 'b', 'c', 'd'], dtype=object)
In [322]: np.argwhere(cols=='a')
Out[322]: array([[0]])
In [323]: np.delete(cols, _)
Out[323]: array(['b', 'c', 'd'], dtype=object)
In [324]: df[_]
Out[324]:
b c d
0 1 1 1
1 1 1 1
In [326]: df[_323].sum(axis=1)
Out[326]:
0 3
1 3
dtype: int64
For two arrays a and b,
a = np.array([[1],[2],[3],[4]])
b = np.array(['a', 'b', 'c', 'd'])
I want to generate the following array
c = np.array([[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd']])
Is there a way to do this efficiently ?
You need:
import numpy as np
a = np.array([[1],[2],[3],[4]])
b = np.array(['a', 'b', 'c', 'd'])
print(np.array(list(zip(np.concatenate(a), b))))
Output:
[[1, 'a'], [2, 'b'], [3, 'c'], [4, 'd']]
Alternate Solution
print(np.stack((np.concatenate(a), b), axis=1))
Solution
>>> import numpy as np
>>> a = np.array([[1],[2],[3],[4]])
>>> b = np.array(['a', 'b', 'c', 'd'])
# You have strange array so result is strange
>>> np.array([[a[i],b[i]] for i in range(a.shape[0])])
array([[array([1]), 'a'],
[array([2]), 'b'],
[array([3]), 'c'],
[array([4]), 'd']], dtype=object)
# You want this
>>> np.array([[a[i][0],b[i]] for i in range(a.shape[0])])
array([['1', 'a'],
['2', 'b'],
['3', 'c'],
['4', 'd']], dtype='<U11')
>>>
Note:
You may want to reshape your 'a' array.
>>> a.shape
(4, 1)
>>> a
array([[1],
[2],
[3],
[4]])
Reshape like this for easier use, for next time...
>>> a.reshape(4)
array([1, 2, 3, 4])
You can do:
c = np.vstack((a.flatten(), b)).T
Is not exactly like a matrix transpose. I'm using python and trying using matrix transformations but I can't without loops, I'm using numpy, is there any solution just using matrix operations or vectorized functions?.
For example:
To this
Looks like you want to rotate this 180 degrees then transpose. How about:
x = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
>>> array([[1, 2, 3],
[4, 5, 6],
[7, 8, 9]])
np.rot90(x, 2).T
>>> array([[9, 6, 3],
[8, 5, 2],
[7, 4, 1]])
Here are is a way that only uses indexing:
>>> import numpy as np
>>> a = np.array(['abcdefghi']).view('U1').reshape(3, 3)
>>> a
array([['a', 'b', 'c'],
['d', 'e', 'f'],
['g', 'h', 'i']], dtype='<U1')
>>>
>>> a[[2,1,0],[[2],[1],[0]]]
array([['i', 'f', 'c'],
['h', 'e', 'b'],
['g', 'd', 'a']], dtype='<U1')
If you do not want to hardcode the indices you'll have to use some kind of builtin. Either Python builtins:
>>> a[list(reversed(range(3))), list(zip(reversed(range(3))))]
array([['i', 'f', 'c'],
['h', 'e', 'b'],
['g', 'd', 'a']], dtype='<U1')
or numpy
>>> a[np.ogrid[2:-1:-1,2:-1:-1][::-1]]
array([['i', 'f', 'c'],
['h', 'e', 'b'],
['g', 'd', 'a']], dtype='<U1')
Note that all these methods do a non-lazy transpose, meaning that the resulting array is C contiguous.