Categories
numpy pandas python

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 I’m hoping to extract an array of data, one value from each row. The array of column names specifies which column I want from each row. Here, the result would be:

[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 got a segfault, which I couldn’t diagnose because the documentation is lacking.