问题
I've got a Series of numpy arrays:
import pandas as pd
import numpy as np
pd.Series({10: np.array([[0.72260683, 0.27739317, 0. ],
[0.7187053 , 0.2812947 , 0. ],
[0.71435467, 0.28564533, 1. ],
[0.3268072 , 0.6731928 , 0. ],
[0.31941951, 0.68058049, 1. ],
[0.31260015, 0.68739985, 0. ]]),
20: np.array([[0.7022099 , 0.2977901 , 0. ],
[0.6983866 , 0.3016134 , 0. ],
[0.69411673, 0.30588327, 1. ],
[0.33857735, 0.66142265, 0. ],
[0.33244109, 0.66755891, 1. ],
[0.32675582, 0.67324418, 0. ]]),
38: np.array([[0.68811957, 0.31188043, 0. ],
[0.68425783, 0.31574217, 0. ],
[0.67994496, 0.32005504, 1. ],
[0.34872593, 0.65127407, 0. ],
[0.34276171, 0.65723829, 1. ],
[0.33722803, 0.66277197, 0. ]])}
)
and an array of indices np.array([1, 4, 1])
indicating which rows should be filtered from consecutive arrays. The expected output would be like this:
pd.Series({10: np.array([[0.7187053 , 0.2812947 , 0. ]]),
20: np.array([[0.33244109, 0.66755891, 1. ]]),
38: np.array([[0.68425783, 0.31574217, 0. ]])}
)
How can I do that? How it would be different if I would like to take out third element from each resulting array obtaining the following Series?
pd.Series({10: 0, 20: 1, 30: 0})
回答1:
If possible convert Series of 2d arrays to 3d array (same length)s of each 2d arrays:
a = np.array([1, 4, 1])
b = np.array(s.tolist())[np.arange(len(s)), a, 2]
print (b)
[0. 1. 0.]
c = pd.Series(b, index=s.index)
print (c)
10 0.0
20 1.0
38 0.0
dtype: float64
If want select by array of indices:
b1 = np.array(s.tolist())[np.arange(len(s)), a]
print (b1)
[[0.7187053 0.2812947 0. ]
[0.33244109 0.66755891 1. ]
[0.68425783 0.31574217 0. ]]
来源:https://stackoverflow.com/questions/61042740/how-to-select-elements-from-subsequent-numpy-arrays-stored-in-pandas-series