How to use merge in pandas
<p>Sorry guys, I know it is a very basic question, I'm just a beginner</p>

<pre><code>In [55]: df1
Out[55]:
   x  y
a  1  3
b  2  4
c  3  5
d  4  6
e  5  7

In [56]: df2
Out[56]:
   y  z
b  1  9
c  3  8
d  5  7
e  7  6
f  9  5
</code></pre>

<p>pd.merge(df1, df2) gives:</p>

<pre><code>In [56]: df2
Out[56]:
   x  y  z 
0  1  3  8
1  3  5  7
2  5  7  6
</code></pre>

<p>I'm confused the use of merge, what does '0','1','2' mean? For example,when the index is 0, why x is 1, y is 3 and z is 8?</p>

<p>You get that due to defaults for <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.merge.html" rel="nofollow"><code>pd.merge</code></a>:</p>

<blockquote>
<pre><code>merge(left, right, how='inner', on=None, left_on=None, right_on=None,
left_index=False, right_index=False, sort=False, suffixes=('_x',
'_y'), copy=True, indicator=False)

on : label or list
    Field names to join on. Must be found in both DataFrames. If on is
    None and not merging on indexes, then it merges on the intersection of
    the columns by default.
</code></pre>
</blockquote>

<p>You haven't pass any key to <code>on</code> key, so it merges on the intersection of the columns by default. You have different indices for your <code>df1</code> and <code>df2</code> so if you want to keep left or right you should specify that:</p>

<pre><code>In [43]: pd.merge(df1, df2)
Out[43]:
   x  y  z
0  1  3  8
1  3  5  7
2  5  7  6

In [44]: pd.merge(df1, df2, on='y', left_index=True)
Out[44]:
   x  y  z
c  1  3  8
d  3  5  7
e  5  7  6

In [45]: pd.merge(df1, df2, on='y', right_index=True)
Out[45]:
   x  y  z
a  1  3  8
c  3  5  7
e  5  7  6
</code></pre>

python pandas