且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

python大 pandas 数据框 - 无法弄清楚如何查找从df给出值的索引

更新时间:2023-11-27 23:18:34

您可以使用其他索引编制系列方式:

  mapA = pd.Series(data.index,index = data.A)

然后 mapA [rollmax.ix ['j','A']] 给出'g'


I have 2 dataframes of numerical data. Given a value from one of the columns in the second df, I would like to look up the index for the value in the first df. More specifically, I would like to create a third df, which contains only index labels - using values from the second to look up its coordinates from the first.

listso = [[21,101],[22,110],[25,113],[24,112],[21,109],[28,108],[30,102],[26,106],[25,111],[24,110]]
data = pd.DataFrame(listso,index=list('abcdefghij'), columns=list('AB'))
rollmax = pd.DataFrame(data.rolling(center=False,window=5).max())

So for the third df, I hope to use the values from rollmax and figure out which row they showed up in data. We can call this third df indexlookup.

For example, rollmax.ix['j','A'] = 30, so indexlookup.ix['j','A'] = 'g'.

Thanks!

You can build a Series with the indexing the other way around:

mapA = pd.Series(data.index, index=data.A)

Then mapA[rollmax.ix['j','A']] gives 'g'.