Access entries in pandas data frame using a list of indices

走远了吗. 提交于 2019-12-19 09:45:13

问题


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


来源:https://stackoverflow.com/questions/45953453/access-entries-in-pandas-data-frame-using-a-list-of-indices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!