查找数据帧的索引与另一个数据帧的比较

时间:2018-03-07 09:14:24

标签: python pandas dataframe

我有两个数据帧df和df1。两者都有一个名为description的列(可能不是唯一的)。我想获得df的索引号,其中描述符合df1的描述。

df      
    Name    des
0   xyz1    abc
1   xyz2    bcd
2   xyz3    nna
3   xyz4    mmm
4   xyz5    man



df1     
    des 
0   abc 
1   nna 
2   bcd 
3   man 


O/P required        
df1     
    des index_df
0   abc 0
1   nna 2
2   bcd 1
3   man 4

2 个答案:

答案 0 :(得分:1)

可以使用.loc访问者并使用reset_index将索引提升为列:

res = df.loc[df['des'].isin(set(df1['des'])), 'des'].reset_index()

#    index  des
# 0      0  abc
# 1      1  bcd
# 2      2  nna
# 3      4  man

答案 1 :(得分:0)

使用Series des对交换的索引和列s = pd.Series(df.index, index=df['des']) df1['index_df'] = df1['des'].map(s) print (df1) des index_df 0 abc 0 1 nna 2 2 bcd 1 3 man 4 创建的值:

A
相关问题