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

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 this with column names:

['a', 'a', 'b', 'c', 'b']

and Im hoping to extract an array of data, one from each row. An array of column names indicates which column I want from each row. Here is the result:

[1, 4, 8, 12, 14]

Is this possible as a single command with Pandas, or do I need to iterate? I tried using indexing

i = pd.Index(['a', 'a', 'b', 'c', 'b'])
i.choose(df)

but I have a segfault that I could not diagnose because the documentation is not enough.

+4
source share
3 answers

You can use lookupfor example

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

where i.indexmay be different from range(len(i))if you want.

+13

numpy, ( ):

df.values[arange(5),[0,0,1,2,1]]

out: array([ 1,  4,  8, 12, 14])

, .

+1

You can always use list comprehension:

[df.loc[idx, col] for idx, col in enumerate(['a', 'a', 'b', 'c', 'b'])]
0
source

All Articles