how to match two dataFrame in python
<p>I have two dataFrame in Python.
The first one is df1:</p>

<pre><code>'ID'    'B' 
   AA    10
   BB    20
   CC    30
   DD    40
</code></pre>

<p>The second one is df2:</p>

<pre><code> 'ID'  'C'  'D'
    BB   30   0
    DD   35   0
</code></pre>

<p>What I want to get finally is like df3:</p>

<pre><code>'ID'  'C'  'D'
   BB   30   20
   DD   35   40
</code></pre>

<p>how to reach this goal?
my code is:</p>

<pre><code>for i in df.ID
  if len(df2.ID[df2.ID==i]):
    df2.D[df2.ID==i]=df1.B[df2.ID==i]
</code></pre>

<p>but it doesn't work.</p>

<p>Following code will replace zero in df1 with value df2</p>

<pre><code>df1=pd.DataFrame(['A','B',0,4,6],columns=['x'])
df2=pd.DataFrame(['A','X',3,0,5],columns=['x'])
df3=df1[df1!=0].fillna(df2)
</code></pre>

python dataframe