问题
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