使用类似于.isin的方法,但返回行号



我想得到类似.isin的匹配条件的行号,数据帧的一部分显示如下

df1

0    452525
1    110101
2    393910

df2

0     100000
1     110000
2     110100
3     110101
4     110102
5     110105
6     110106
7     110107
8     110108
9     110109
10    110111
11    110112
12    110113
13    110114
14    110115

当我使用时

t1.isin(t2)

我有

0    False
1     True
2    False

我想要的输出是

0    NaN
1    3
2    NaN

有办法做到这一点吗?但我想避免使用pd.merge((,因为这两个数据帧都很大,如果合并它们需要很长时间才能执行

通过字典使用Series.map以及t2:的交换值和索引

#if t1, t2 are Series
out = t1.map(dict(zip(t2, t2.index)))
#if t1, t2 are Dataframes
out = t1['col'].map(dict(zip(t2['col'], t2.index)))
print (out)
0    NaN
1    3.0
2    NaN
Name: col, dtype: float64

假设t1t2系列,我将使用merge:

t1.to_frame().merge(t2.reset_index(), how='left')['index']

输出:

0    NaN
1    3.0
2    NaN
Name: index, dtype: float64

从数据帧CCD_ 6和CCD_;col";作为列名:

df1.merge(df2.reset_index(), on='col', how='left')['index']

最新更新