Access entries in pandas data frame using a list o

2019-02-18 19:15发布

问题:

I facing the issue that I need only a subset of a my original dataframe that is distributed over different rows and columns. E.g.:

# My Original dataframe
import pandas as pd
dfTest = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]])

Output:

   0  1  2
0  1  2  3
1  4  5  6
2  7  8  9

I can provide a list with rows and column indices where my desired values are located:

array_indices = [[0,2],[1,0],[2,1]]

My desired output is a series:

3
4
8

Can anyone help?

回答1:

Use pd.DataFrame.lookup

dfTest.lookup(*zip(*array_indices))

array([3, 4, 8])

Which you can wrap in a pd.Series constructor

pd.Series(dfTest.lookup(*zip(*array_indices)))

0    3
1    4
2    8
dtype: int64

Slight variant

i, j = np.array(array_indices).T
dfTest.values[i, j]

array([3, 4, 8])

Similarly as above

pd.Series(dfTest.values[i, j])

0    3
1    4
2    8
dtype: int64