两个数据帧:df1
,df2
。
如果df2.index中的df1.index,则df1 ['mask'] = 1,否则df ['mask'] = 0
df3
是预期的结果。
import pandas as pd
data1 = {'key':['a','b','c','d','e','f','g'], 'profit':[12,3,4,5,6,7,9]}
df1 = pd.DataFrame(data1)
data2= {'key':['a','b','c'], 'profit':[12,3,4]}
df2 = pd.DataFrame(data2)
data3 = {'key':['a','b','c','d','e','f','g'], 'profit':[12,3,4,5,6,7,9],
'mask':[1,1,1,0,0,0,0]}
df3=pd.DataFrame(data3)