I have the following script and sample data:
import numpy as np
from tabulate import tabulate
a = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
table = [['cat','value'],[1,0.5],[2,0.5],[3,0.5],[4,0.5],[6,0.05],[7,0.025],[8,0.005],[9,0.123]]
print(tabulate(table))
Output:
cat value
1 0.5
2 0.5
3 0.5
4 0.5
6 0.05
7 0.025
8 0.005
9 0.123
The array (a
) contains the categories.
How can I replace the categories in the array with the data from the "value"
column in the table?
For example, all 1 in the array will be replaced with 0.5, etc.
In this example, I created a table. But I also have a separate csv file containing the data from the table I created.
I will be applying this on a 1200 x 1200 array.
You can create a mapping dictionary and then apply this mapping to your list. For example:
mapping = dict(table[1:])
out = [[mapping.get(v, v) for v in l] for l in a]
print(out)
Prints:
[[0.5, 0.5, 0.5], [0.5, 5, 0.05], [0.025, 0.005, 0.123]]