How can I select a specific column from each row in a Pandas DataFrame?

前端 未结 3 1405
走了就别回头了
走了就别回头了 2021-01-11 12:25

I have a DataFrame in this format:

    a   b   c
0   1   2   3
1   4   5   6
2   7   8   9
3   10  11  12
4   13  14  15

and an array like

3条回答
  •  失恋的感觉
    2021-01-11 12:36

    You could use lookup, e.g.

    >>> i = pd.Series(['a', 'a', 'b', 'c', 'b'])
    >>> df.lookup(i.index, i.values)
    array([ 1,  4,  8, 12, 14])
    

    where i.index could be different from range(len(i)) if you wanted.

提交回复
热议问题