Pandas Equivalent of R's which()

前端 未结 6 1577
广开言路
广开言路 2020-12-31 04:12

Variations of this question have been asked before, I\'m still having trouble understanding how to actually slice a python series/pandas dataframe based on conditions that

6条回答
  •  借酒劲吻你
    2020-12-31 04:47

    What what I know of R you might be more comfortable working with numpy -- a scientific computing package similar to MATLAB.

    If you want the indices of an array who values are divisible by two then the following would work.

    arr = numpy.arange(10)
    truth_table = arr % 2 == 0
    indices = numpy.where(truth_table)
    values = arr[indices]
    

    It's also easy to work with multi-dimensional arrays

    arr2d = arr.reshape(2,5)
    col_indices = numpy.where(arr2d[col_index] % 2 == 0)
    col_values = arr2d[col_index, col_indices]
    

提交回复
热议问题