Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select values from a dataframe by a series of column names?

I have a dataframe df:

    A   B
0   1   4
1   2   5
2   3   6

And a series s:

0    A
1    B
2    A

Now I want to pick values from df with column names specified in s. The expected result is:

0    1  <- from column A
1    5  <- from column B
2    3  <- from column A

How can I get this done efficiently?

like image 674
Chen Avatar asked Dec 16 '25 13:12

Chen


1 Answers

Use Index.get_indexer for indices by Series and select values by numpy indexing in 2d array:

a = df.to_numpy()
b = a[np.arange(len(df)), df.columns.get_indexer(s)]
print (b)
[1 5 3]

s1 = pd.Series(b, s.index)
print (s1)
0    1
1    5
2    3
dtype: int64
like image 196
jezrael Avatar answered Dec 19 '25 04:12

jezrael



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!