0
test_arr = pd.Series(["Abby", "Michael","Abby"])
test_arr[test_arr.map(lambda x: x.upper() == "ABBY")]

I don't understand how this works map works with the subscript i.e. []:

map_results = test_arr.map(lambda x: x.upper() == "ABBY")
test_arr[map_results]

I printed the output of map_results function and it returns an array of boolean. How does this [TRUE, FALSE, TRUE] map to an index/element exactly? Does pandas override the susbcript?

Lews Therin
  • 10,907
  • 4
  • 48
  • 72
  • In pandas/numpy we can use `bool` arrays/Series with the same size of the data we want to slice, to select target elements. It's like a `foor loop`, but vectorized (therefore, much faster), where we get only the values corresponding to `True` in the boolean array. – Cainã Max Couto-Silva Dec 23 '20 at 05:30
  • @CainãMaxCouto-Silva what if another thread manipulates the order of the original series? I don't know if it's possible just wondering. – Lews Therin Dec 23 '20 at 06:06
  • Actually, I don't know if some operations in pandas/numpy does multiprocessing. If so, probably they split the data in ordered chunks for each thread, and returns the output ordered as well. It's the case when using a common [multi threading approach in Python](https://stackoverflow.com/questions/41273960/python-3-does-pool-keep-the-original-order-of-data-passed-to-map) at least. – Cainã Max Couto-Silva Dec 23 '20 at 06:17

0 Answers0