I have a pandas Series like this:
measure
0 0.3
6 0.6
9 0.2
11 0.3
14 0.0
17 0.1
23 0.9
and a numpy array like this:
array([[ 0, 0, 9, 11],
[ 6, 14, 6, 17]])
How can I do a lookup from the values in the numpy array to the indices in the series to get this:
array([[ 0.3, 0.3, 0.2, 0.3],
[ 0.6, 0.0, 0.6, 0.1]])
You can convert pandas dataframe to numpy array using the df. to_numpy() method. Numpy arrays provide fast and versatile ways to normalize data that can be used to clean and scale the data during the training of the machine learning models.
The Pandas module mainly works with the tabular data, whereas the NumPy module works with the numerical data. The Pandas provides some sets of powerful tools like DataFrame and Series that mainly used for analyzing the data, whereas in NumPy module offers a powerful object called Array.
Pandas expands on NumPy by providing easy to use methods for data analysis to operate on the DataFrame and Series classes, which are built on NumPy's powerful ndarray class.
Via np.vectorize
, with series s
and array a
:
np.vectorize(s.get)(a)
Using replace
a=np.array([[ 0, 0, 9, 11],
[ 6, 14, 6, 17]])
pd.DataFrame(a).replace(df.measure.to_dict()).values
Out[214]:
array([[0.3, 0.3, 0.2, 0.3],
[0.6, 0. , 0.6, 0.1]])
Interesting way using np.bincount
np.bincount(s.index.values, s.values)[a]
array([[ 0.3, 0.3, 0.2, 0.3],
[ 0.6, 0. , 0.6, 0.1]])
Setup
s = pd.Series(
[.3, .6, .2, .3, .0, .1, .9],
[0, 6, 9, 11, 14, 17, 23]
)
a = np.array([
[0, 0, 9, 11],
[6, 14, 6, 17]
])
You can use loc and reshape:
s = pd.Series({0: 0.3, 6: 0.6, 9: 0.2, 11: 0.3, 14: 0.0, 17: 0.1, 23: 0.9})
a = np.array([[ 0, 0, 9, 11],
[ 6, 14, 6, 17]])
s.loc[a.flatten()].values.reshape(a.shape)
Out[192]:
array([[ 0.3, 0.3, 0.2, 0.3],
[ 0.6, 0. , 0.6, 0.1]])
Or:
pd.DataFrame(a).applymap(lambda x: s.loc[x]).values
Out[200]:
array([[ 0.3, 0.3, 0.2, 0.3],
[ 0.6, 0. , 0.6, 0.1]])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With