Skip to content Skip to sidebar Skip to footer

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',

Solution 1:

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.

Solution 2:

For large datasets, you can use indexing on the base numpy data, if you're prepared to transform your column names into a numerical index (simple in this case):

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

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

This will be much more efficient that list comprehensions, or other explicit iterations.

Solution 3:

You can always use list comprehension:

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

Post a Comment for "How Can I Select A Specific Column From Each Row In A Pandas Dataframe?"