Python Pandas: Json to Excel just one row exports issue - python

I have a issue with Python Pandas. My code is showing first element but not showing another. How can I solve this issue? I shared a screenshot. My code added just Admin parent, HAKAN MISIRLI child.
My Json:
{
"ADMİN" : {
"HAKAN MISIRLI" : [ 33333333333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
},
"AKPINAR GES" : {
"BAHRİ KAŞARCI" : [ 44444444444, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
},
"Gölbaşı Ges" : {
"MOLLA AHMET ŞAHİN " : [ 555555555555, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
}
}
My code:
url = "the url of my Json file "
with urllib.request.urlopen(url + ".json") as url:
data = json.loads(url.read().decode())
role = list(data.keys())[0]
name = list(data[role].keys())[0]
listson = data[role][name]
columns = ["Saha", "İsim", "TC Kimlik"] + list(range(1, len(listson)))
rows = ["Personel"]
dfson1 = pd.DataFrame([role, name] + listson).T
dfson1.columns = columns
dfson1.index = rows
dfson1.to_excel("export.xlsx")
Export from this code:
screenshot from my code export

Doesn't look like you looped over the json as it's not in a list. Here's an alternative to consider. You can go line by line to see what the code is doing.
Try:
data = '''{
"ADMİN" : {
"HAKAN MISIRLI" : [ 33333333333, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
},
"AKPINAR GES" : {
"BAHRİ KAŞARCI" : [ 44444444444, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
},
"Gölbaşı Ges" : {
"MOLLA AHMET ŞAHİN " : [ 555555555555, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]
}
}'''
dataj = json.loads(data)
df = pd.concat([pd.json_normalize(j) for j in [dataj]])
df1 = df.T.reset_index()
df1[["Saha", "İsim"]] = df1['index'].str.split('.', expand=True)
dft = pd.DataFrame(df1[0].tolist())
df_final = pd.concat([df1[["Saha", "İsim"]], dft], axis=1)
df_final.rename(columns={0:'TC Kimlik'}, inplace=True)
Ouput
Saha İsim TC Kimlik 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
0 ADMİN HAKAN MISIRLI 33333333333 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
1 AKPINAR GES BAHRİ KAŞARCI 44444444444 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0
2 Gölbaşı Ges MOLLA AHMET ŞAHİN 555555555555 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Related

loop though which I need to get/add 0's in the start

I have an array list values as below
import numpy as np
lst = np.array([5.93442446e-01, 2.41275693e-01, 9.80953766e-02, 3.98826039e-02,
1.62150567e-02, 6.59255008e-03, 2.68033084e-03, 1.08974120e-03,
4.43055708e-04, 1.80133009e-04, 7.32366160e-05, 2.97757861e-05])
data = np.array([100, 200, 300, 400, 500, 600, 700, 800, 900, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0])
And using these array lists I have multiplied with data below
decayed_value = []
for i in range(len(data)):
transformed_data = lst * data[i]
decayed_value.append(np.int64(transformed_data))
If you see I have got the decayed_value but I want these values with 0's in the start as the data goes on.
Expected Output:
[array([59, 24, 9, 3, 1, 0, 0, 0, 0, 0, 0, 0], dtype=int64),
array([0, 118, 48, 19, 7, 3, 1, 0, 0, 0, 0, 0, 0],
dtype=int64),
array([0, 0, 178, 72, 29, 11, 4, 1, 0, 0, 0, 0, 0, 0],
dtype=int64),
array([0, 0, 0, 237, 96, 39, 15, 6, 2, 1, 0, 0, 0, 0, 0],
dtype=int64),
array([0, 0, 0, 0, 296, 120, 49, 19, 8, 3, 1, 0, 0, 0, 0, 0],
dtype=int64),
....
...
..
.]
I need to get 0's in the above way. How do I do this?
It looks like you want to manipulate the numpy array.
One way to do this would be to use numpy.insert option. Since you are looking to extract each value in data and multiply with the lst array, you can do it this way. I am sure there are more efficient ways to do it. This is a quick and dirty option for you to consider.
import numpy as np
lst = np.array([5.93442446e-01, 2.41275693e-01, 9.80953766e-02, 3.98826039e-02,
1.62150567e-02, 6.59255008e-03, 2.68033084e-03, 1.08974120e-03,
4.43055708e-04, 1.80133009e-04, 7.32366160e-05, 2.97757861e-05])
data = np.array([100, 200, 300, 400, 500, 600, 700, 800, 900, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0])
for i,d in enumerate(data):
lst_x = (lst * d).astype(int)
if i > 0:
lst_x = np.insert(lst_x,0,[0,]*i)[:len(lst)]
print (lst_x)
Output of this will be:
[ 59 24 9 3 1 0 0 0 0 0 0 0]
[ 0 118 48 19 7 3 1 0 0 0 0 0]
[ 0 0 178 72 29 11 4 1 0 0 0 0]
[ 0 0 0 237 96 39 15 6 2 1 0 0]
[ 0 0 0 0 296 120 49 19 8 3 1 0]
[ 0 0 0 0 0 356 144 58 23 9 3 1]
[ 0 0 0 0 0 0 415 168 68 27 11 4]
[ 0 0 0 0 0 0 0 474 193 78 31 12]
[ 0 0 0 0 0 0 0 0 534 217 88 35]
[ 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0]
If you want these to be stored into a list (decayed_value), then you can add this to your code:
decayed_value = []
for i,d in enumerate(data):
lst_x = (lst * d).astype(int)
if i > 0:
lst_x = np.insert(lst_x,0,[0,]*i)[:len(lst)]
#print (lst_x)
decayed_value.append(lst_x)
print (decayed_value)
Output of this will be:
[array([59, 24, 9, 3, 1, 0, 0, 0, 0, 0, 0, 0]),
array([ 0, 118, 48, 19, 7, 3, 1, 0, 0, 0, 0, 0]),
array([ 0, 0, 178, 72, 29, 11, 4, 1, 0, 0, 0, 0]),
array([ 0, 0, 0, 237, 96, 39, 15, 6, 2, 1, 0, 0]),
array([ 0, 0, 0, 0, 296, 120, 49, 19, 8, 3, 1, 0]),
array([ 0, 0, 0, 0, 0, 356, 144, 58, 23, 9, 3, 1]),
array([ 0, 0, 0, 0, 0, 0, 415, 168, 68, 27, 11, 4]),
array([ 0, 0, 0, 0, 0, 0, 0, 474, 193, 78, 31, 12]),
array([ 0, 0, 0, 0, 0, 0, 0, 0, 534, 217, 88, 35]),
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]),
array([ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])]

How can I add a small matrix into a bigger one along the diagonal in a specific way?

I am trying to write a big matrix that includes a smaller row matrix (size changeable) that are spread on the "diagonal" of the matrix. All the other values are 0. How do I create such a matrix?
I've tried np.put, np.append. Here's what I have so far:
t = [1,2,3]
n=3
m=4
A = np.zeros((2*m,m*n+m),dtype=int)
for i in range (m):
A[i-1:i-1+t.shape[0], n*(i-1):n*(i-1)+t.shape[1]] += t
print("A= \n",np.matrix(A))
I want the following matrix (I'm sorry I don't know how to show matrix but if someone can help me with this too I would appreciate it a lot) :
A=
[[1 2 3 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 1 2 3 0 0 0 0 0 0 0 0 0 0 ]
[0 0 0 0 0 0 1 2 3 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 1 2 3 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]]
It causes the following error:
ValueError: operands could not be broadcast together with shapes (0,0) (1,3) (0,0)
You can use careful reshaping like so:
t = [1,2,3]
n=3
m=4
A = np.zeros((2*m,m*n+m),dtype=int)
A.ravel()[:m*(m*n+m+n)].reshape(m,-1)[:,:len(t)] = t
A
# array([[1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
# [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])
Make mask for 12 positions and use it for assignment
idx = np.zeros(A.shape).astype(bool)
for i in range(m):
idx[i,i*n:i*n+3] = True
A[idx]= t*m
array([[1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

How to find most common elemtent in a ndarray [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
I have a numpy array with the following shape (11617, 37). The data is multi class data, and to establish a baseline, I need to find which class (or classes) are the most common.
I have tried this formula and also this
A = np.array([[0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0],
[0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0],
[0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0]])
axis = 0
u, indices = np.unique(arr, return_inverse=True)
answer = u[np.argmax(np.apply_along_axis(np.bincount, axis, indices.reshape(arr.shape),
None, np.max(indices) + 1), axis=axis)]
I need to find the most frequent combination of the 37 classes in my array
Expected output:
[0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0]
To find the most frequent combination (rows, which means axis=0), you can try this!
A = np.array([[1,0,0,0],
[1,0,0,1],
[1,0,0,0]])
unique_rows,counts = np.unique(A, return_counts=True,axis=0)
unique_rows[np.argmax(counts)]
FYI, If the array you mentioned in the question is your target variable, then it is an example of multi-label data.
This may be of use for you to understand multi-class and multi-label
You could try np.unique with return_counts parameter:
from operator import itemgetter
import numpy as np
A = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
uniques, counts = np.unique(A, axis=0, return_counts=True)
idxmax, _ = max(zip(range(len(counts)), counts), key=itemgetter(1))
print(uniques[idxmax])
Output
[0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0]
You can use collections.Counter.most_common if you convert your list of list elements to a tuple (convert the lists to tuples so they can be counted)
from collections import Counter
A = [[0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0]]
c = Counter(tuple(x) for x in A)
print(c.most_common()[0]) # ((0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 1, 0, 0, 0), 2)
This returns a tuple containing the most common list and the number of occurrences.
A really quick and easy solution:
A = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
print(max(A, key=A.count))
Which prints:
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0]
If you need to pay attention to runtime or want to optimize your code - this is not the way you want to go. However, if you just need a quick solution, it might help to keep this one-liner in mind.
(A.tolist() gets you a list from a np.ndarray if you need that first.)
from collections import Counter
A = [[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
most_common = [Counter(i).most_common(1).pop()[0] for i in A]
most_common
[0, 0, 0]

Using numpy.reshape in python

I am trying to use numpy.reshape to get a matrix of 2x100. I have a list having 200 elements. Here is my code-
vec is my list containing 200 elements-
[1, 1, 1, 1, 1, 0, 1, 0, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 1, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 0, 1, 0, 1, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Here is my code-
data=np.array(vec)
shape = ( 2, 100 )
data.reshape(shape)
print(data)
But I do not get a 2x100 matrix. This is what I get-
[1 1 1 1 1 0 1 0 1 1 1 0 1 1 1 1 1 1 1 1 1 0 0 0 0 0 1 0 1 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 1 0
0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0 1 1 1 1 1 1 1 1 0 0
1 0 0 0 0 0 1 0 0 0 0 0 1 1 1 0 1 1 1 0 0 0 1 0 0 0 1 0 0 0 0 1 1 0 0 0 0
1 0 0 0 1 1 0 0 1 0 0 0 0 0 0 1 1 0 0 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
reshape does not modify the array. It returns a new reshaped array. Use data = data.reshape(shape).

Mask a 2D Numpy Array by array of indexes like np.in1d for 2d arrays

np.array(
[[0,13,0,2,0,0,0,0,0,0,0,0],
[0,0,15,0,9,0,0,0,0,0,0,0],
[0,0,0,0,0,18,0,0,0,0,0,0],
[0,0,0,0,27,0,20,0,0,0,0,0],
[0,0,0,0,0,20,0,10,0,0,0,0],
[0,0,0,0,0,0,0,0,8,0,0,0],
[0,0,0,0,0,0,0,14,0,14,0,0],
[0,0,0,0,0,0,0,0,12,0,25,0],
[0,0,0,0,0,0,0,0,0,0,0,11],
[0,0,0,0,0,0,0,0,0,0,15,0],
[0,0,0,0,0,0,0,0,0,0,0,7],
[0,0,0,0,0,0,0,0,0,0,0,0]])
I am trying to find how to take a numpy array like above and then in one performant operation mask it with indexes of elements I want zeroed
[0,1]
[1,4]
[4,7]
[7,8]
[8,11]
So what I am left with is
np.array(
[[0,0,0,2,0,0,0,0,0,0,0,0],
[0,0,15,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,18,0,0,0,0,0,0],
[0,0,0,0,27,0,20,0,0,0,0,0],
[0,0,0,0,0,20,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,8,0,0,0],
[0,0,0,0,0,0,0,14,0,14,0,0],
[0,0,0,0,0,0,0,0,0,0,25,0],
[0,0,0,0,0,0,0,0,0,0,0,0],
[0,0,0,0,0,0,0,0,0,0,15,0],
[0,0,0,0,0,0,0,0,0,0,0,7],
[0,0,0,0,0,0,0,0,0,0,0,0]])
Something like the functionality of np.in1d but for a 2d array? I can iterate over each element but the arrays can be truly massive so vector single operation mask would be best. Is it possible? If this is a stupid question I'm sure I'll be told!
You can directly access these indexes in the following way
indexes = [[0,1], [1,4], [4,7], [7,8], [8,11]]
indexes =zip(*indexes)
>>[(0, 1, 4, 7, 8), (1, 4, 7, 8, 11)]
a[indexes[0], indexes[1]]=0
>>
[[ 0 0 0 2 0 0 0 0 0 0 0 0]
[ 0 0 15 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 18 0 0 0 0 0 0]
[ 0 0 0 0 27 0 20 0 0 0 0 0]
[ 0 0 0 0 0 20 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 8 0 0 0]
[ 0 0 0 0 0 0 0 14 0 14 0 0]
[ 0 0 0 0 0 0 0 0 0 0 25 0]
[ 0 0 0 0 0 0 0 0 0 0 0 0]
[ 0 0 0 0 0 0 0 0 0 0 15 0]
[ 0 0 0 0 0 0 0 0 0 0 0 7]
[ 0 0 0 0 0 0 0 0 0 0 0 0]]
I think you look for this
a = np.array(
[[0,13,0,2,0,0,0,0,0,0,0,0],
[0,0,15,0,9,0,0,0,0,0,0,0],
[0,0,0,0,0,18,0,0,0,0,0,0],
[0,0,0,0,27,0,20,0,0,0,0,0],
[0,0,0,0,0,20,0,10,0,0,0,0],
[0,0,0,0,0,0,0,0,8,0,0,0],
[0,0,0,0,0,0,0,14,0,14,0,0],
[0,0,0,0,0,0,0,0,12,0,25,0],
[0,0,0,0,0,0,0,0,0,0,0,11],
[0,0,0,0,0,0,0,0,0,0,15,0],
[0,0,0,0,0,0,0,0,0,0,0,7],
[0,0,0,0,0,0,0,0,0,0,0,0]])
b = np.array([[0,1],[1,4],[4,7],[7,8],[8,11]])
# get x coordinates in an array
c1 = b[:,0]
# get y coordinates in an array
c2 = b[:,1]
a[c1[:,None],c2] = 0
a
array([[ 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 15, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 18, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 27, 0, 20, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 20, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 8, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 14, 0, 14, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 25, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 15, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]])

Categories

Resources