【发布时间】:2018-02-12 06:38:15
【问题描述】:
如何根据索引和标题列表从数据帧中获取值?
这些是我拥有的数据框:
a = pd.DataFrame([[1,2,3],[4,5,6],[7,8,9]], columns=['a','b','c'])
referencingDf = pd.DataFrame(['c','c','b'])
基于相同的索引,我正在尝试获取以下数据帧输出:
outputDf = pd.DataFrame([3,6,8])
目前,我尝试了这个,但需要采用对角线值。我很确定有更好的方法:
a.loc[referencingDf.index.values, referencingDf[:][0].values]
【问题讨论】:
-
等一下,你是想得到
a[referencingDf[0]]吗? -
是的,没错!
-
那么,你还有什么问题?
-
我如何引用 a 以基于引用 DF 获得以下 outputDf。本质上是 a[0]['c'], a[1]['c'], a[2]['b']。
标签: python pandas dataframe indexing