Python/Pandas - Merging dataframe with numpy array in a peculiar way
Date : March 29 2020, 07:55 AM
wish helps you Try np.tile() to get repeat your array coef as many times as you need: pd.DataFrame(np.tile(coef, (len(df.index), 1)), columns=['Coef']*5)
|
Python numpy array merging manipulation
Tag : python , By : Matt Watson
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , With a as the array and L as the list, you could simply get a flattened view of the array with np.ravel() and assign values from L by slicing into it, like so - a.ravel()[:len(L)] = L
np.put(a, range(len(L)), L)
In [51]: a
Out[51]:
array([[[91, 18, 74],
[49, 92, 93],
[42, 38, 41],
[27, 24, 69]],
[[14, 72, 49],
[85, 74, 45],
[32, 88, 89],
[12, 85, 60]]])
In [52]: L = [120, 99, 0, 88, 78, 32, 123]
In [53]: a.ravel()[:len(L)] = L
In [54]: a
Out[54]:
array([[[120, 99, 0],
[ 88, 78, 32],
[123, 38, 41],
[ 27, 24, 69]],
[[ 14, 72, 49],
[ 85, 74, 45],
[ 32, 88, 89],
[ 12, 85, 60]]])
|
remove specific elements from numpy array [PREVIOUSLY] selecting elements by data type from a numpy array
Date : March 29 2020, 07:55 AM
hop of those help? Note that numpy.select takes an arbitrary condition list. You could use the condition isinstance(x, str) to check if x is of type str
|
Merging array elements based on other arrays elements in Python
Date : March 29 2020, 07:55 AM
I hope this helps . Use zip_longest to create key-value pairs of elements and then use itertools.groupby to group them together based on the key. >>> from itertools import groupby
>>> l = ['409597', ['You'], '409597', ['matter'], '409597', ['manager'], '809558', ['metro'], '809558', ['station'], '829258', ['bucket'], '829258', ['water']]
>>> [[e[1][0] for e in list(v)] for k,v in groupby(zip_longest(*([iter(l)]*2)), lambda x: x[0])]
[['You', 'matter', 'manager'], ['metro', 'station'], ['bucket', 'water']]
ld = [[{'Instant_ID': 409597}, {'Token': 'You'}], [{'Instant_ID': 409597}, {'Token': 'matter'}], [{'Instant_ID': 409597}, {'Token': 'manager'}], [{'Instant_ID': 809558}, {'Token': 'metro'}], [{'Instant_ID': 809558}, {'Token': 'station'}], [{'Instant_ID': 829258}, {'Token': 'bucket'}], [{'Instant_ID': 829258}, {'Token': 'water'}]]
>>> [[e[1]['Token'] for e in v] for k,v in groupby(ld, lambda x: x[0]['Instant_ID'])]
[['You', 'matter', 'manager'], ['metro', 'station'], ['bucket', 'water']]
|
Fast python algorithm (in numpy or pandas?) to find indices of array elements that match elements in another array
Tag : python , By : user186876
Date : March 29 2020, 07:55 AM
|