擅长:python、mysql、java
<p>将<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.merge.html" rel="nofollow noreferrer">^{<cd1>}</a>与<strong><code>indicator = True</code></strong>和<a href="https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.replace.html" rel="nofollow noreferrer">^{<cd3>}</a>连用
利用<code>_merge</code>列</p>
<pre><code>new_df = (df1.merge(df2,on = ['ticker','date'],how = 'left',indicator = 'in_index')
.replace({'in_index':{'both':'yes','left_only':'no'}})
)
print(new_df)
ticker date price in_index
0 AAPL 2018 150.1 yes
1 AAPL 2019 220.5 yes
2 MSFT 2018 50.6 no
3 MSFT 2019 70.9 yes
</code></pre>