Selecting rows in a dataframe based on the column names of another

那年仲夏 提交于 2021-02-05 07:46:57

问题


Say I have two dfs

df = pd.DataFrame({'A': [1, 2, 3,4,5],
                'B': [2, 4,2,4,5], 'C': [1, -1, 3,5,10],'D': [3, -4,3,7,-3]}, columns=['A', 'B', 'C', 'D'])
df = df.set_index(['A'])

df2 = pd.DataFrame({'A': [1, 2, 3,4,5],
                    'J': ['B', 'B','C','D','C']}, columns=['A', 'J'])
df2 = df2.set_index(['A'])

and I would like to use df2 to select the columns of df row by row in order to get the following dataframe

   sel
1    2      
2    4
3    3
4    7
5   10

where the first two values are from the column B of df, the third from col C, the fourth from col D and the last from col C. Is there a natural way to do it in pandas?


回答1:


Use lookup, indexes have to be same in both df:

print (df.lookup(df2.index, df2['J']))
[ 2  4  3  7 10]

df = pd.DataFrame({'sel':df.lookup(df2.index, df2['J'])}, index=df.index)
print (df)
   sel
A     
1    2
2    4
3    3
4    7
5   10



回答2:


You could also use np.diag:

x, y= df2.reset_index().values.T
df= pd.DataFrame(np.diag(df.loc[x, y].values), columns=['sel'])
print(df)
   sel
0    2
1    4
2    3
3    7
4   10


来源:https://stackoverflow.com/questions/45977523/selecting-rows-in-a-dataframe-based-on-the-column-names-of-another

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