JavaScript
x
12
12
1
{'choice_0': {0: 52, 1: 26, 2: 100, 3: 2, 4: 53},
2
'choice_1': {0: 38, 1: 4, 2: 54, 3: 95, 4: 1},
3
'choice_2': {0: 12, 1: 82, 2: 25, 3: 1, 4: 47},
4
'choice_3': {0: 82, 1: 5, 2: 12, 3: 96, 4: 93},
5
'choice_4': {0: 33, 1: 11, 2: 27, 3: 32, 4: 26},
6
'choice_5': {0: 75, 1: 47, 2: 82, 3: 6, 4: 3},
7
'choice_6': {0: 64, 1: 38, 2: 10, 3: 40, 4: 46},
8
'choice_7': {0: 76, 1: 6, 2: 89, 3: 31, 4: 16},
9
'choice_8': {0: 10, 1: 66, 2: 80, 3: 9, 4: 42},
10
'choice_9': {0: 28, 1: 61, 2: 33, 3: 59, 4: 39},
11
'n_people': {0: 4, 1: 4, 2: 3, 3: 2, 4: 4}}
12
And an array like:
JavaScript
1
7
1
input_arr = (
2
np.array([[ 0, 52],
3
[ 1, 82],
4
[ 2, 27],
5
[ 3, 2],
6
[ 4, 53]]))
7
The first element will be for family_id=0 and column “choice_0” = 52
The second element will be for family_id=1 and column “choice_2” = 82
The third element will be for family_id=2 and column “choice_4” = 27
And I will like to get:
JavaScript
1
6
1
array([[ 0, 0],
2
[ 1, 2],
3
[ 2, 3],
4
[ 3, 0],
5
[ 4, 0])
6
The logic will be:
- For family_id =0 The initial array has a a 52. And I will like to receive a 0 because it belongs to the “choice_0” column.
- For family_id = 1 The initial array has a 82. And I will like to receive a 2 because it belongs to the “choice_2″column.
Note: Number within a row(family_id) can´t be repeated.
I don´t know even what is the title, feel free to change it.
Advertisement
Answer
Suppose you have:
JavaScript
1
15
15
1
df = pd.DataFrame.from_dict({'family_id': {0: 0, 1: 1, 2: 2, 3: 3, 4: 4},
2
'choice_0': {0: 52, 1: 26, 2: 100, 3: 2, 4: 53},
3
'choice_1': {0: 38, 1: 4, 2: 54, 3: 95, 4: 1},
4
'choice_2': {0: 12, 1: 82, 2: 25, 3: 1, 4: 47},
5
'choice_3': {0: 82, 1: 5, 2: 12, 3: 96, 4: 93},
6
'choice_4': {0: 33, 1: 11, 2: 27, 3: 32, 4: 26},
7
'choice_5': {0: 75, 1: 47, 2: 82, 3: 6, 4: 3}})
8
9
10
input_arr = (
11
np.array([[ 0, 52],
12
[ 1, 82],
13
[ 2, 27]])
14
)
15
You can get you desired output using a list comprehension.
JavaScript
1
8
1
output_arrary=np.array([[e[0], df.iloc[i].tolist().index(e[1])-1] for i, e in enumerate(input_arr)])
2
3
print(output_arrary)
4
5
[[0 0]
6
[1 2]
7
[2 4]]
8