Good day.
If I have the following array:
JavaScript
x
2
1
[11, "apples", 22, 11], [12, "pear", 24, 11], [13, "bannana", 18, 11], [14, "pear", 17, 11]
2
How can I change the array to only show data from user pear
? I want to collect all the values from column 1 of user pear
. (12, 14)
Or alrternatively how can I find the values that are unique in colum 2, e.g. apples, pear and bannana. And then filter by pear
to find the data only of pear
. [12, “pear”, 24, 11], [14, “pear”, 17, 11]
What have I tried and vary forms of it:
uniqueRows = np.unique(array, axis=:,1)
This is what I can use to filter if I have the unique values.
JavaScript
1
5
1
new_arr = np.array([[11, "apples", 22, 11], [12, "pear", 24, 11], [13, "bannana", 18, 11], [14, "pear", 17, 11]])
2
new_val = np.array(["pear"])
3
result = np.in1d(new_arr[:, 1], new_val)
4
z = new_arr[result]
5
Advertisement
Answer
Pandas Way
JavaScript
1
24
24
1
import numpy as np
2
import pandas as pd
3
4
new_arr = np.array([[11, "apples", 22, 11], [12, "pear", 24, 11], [13, "banana", 18, 11], [14, "pear", 17, 11]])
5
6
df = pd.DataFrame(new_arr,columns=['A','B','C','D'])
7
8
result = df[df.B=='pear']
9
print(result)
10
'''
11
A B C D
12
1 12 pear 24 11
13
3 14 pear 17 11
14
'''
15
#or
16
17
result_2 = df['B'].drop_duplicates()
18
print(result_2)
19
'''
20
0 apples
21
1 pear
22
2 banana
23
'''
24
However instead of drop_duplicate you can use unique() but this way is faster.