Pandas merge in Python
<p>I planned to merge two CSV files with left merge.  </p>

<p>My left file has 145 rows and its key is id with only two values, 1455726 and 1449533. The file looks like below.  </p>

<pre><code>id  
1455726  
1455726  
1455726  
1455726  
...  
1449533  
1449533  
...  
</code></pre>

<p>My right file has 147,390 rows, and its key is ID. The file looks like below.  </p>

<pre><code>ID       Country  Currency  
22564575 KR       USD  
22799263 CA       USD  
........  
</code></pre>

<p>I want to map the country and currency in right file into my left file with the ID, and the ideal output is  </p>

<pre><code>id      ID      Country Currency  
1455726 1455726 US      USD  
1455726 1455726 US      USD  
........  
1449533 1449533 US      USD  
........  
</code></pre>

<p>There are only two values in my left file, which are 14455726 and 1449533, and they are both available in my right file. However, my code failed to map the value from the right file and returned me the following result.  </p>

<pre><code>id      ID      Country Currency  
1455726 1455726  
1455726 1455726  
........  
1449533 1449533  
........  
</code></pre>

<p>But if I remove all the other rows but the two mapped rows in my right files, my code worked and gave me the result I want. I am not sure why my code didn't work for the original right file with 147K+ rows. I tried to search online, but found no other people with the same issue before, so was wondering if anyone can help me solve this issue. Thank you.</p>

<p>Below is my code:</p>

<pre><code>import pandas as pd
df1 = pd.read_csv('tmp.csv')
df4 = pd.read_csv('test4.csv')
df1 = df1.merge(df4, how='left', left_on=['id'], right_on=['ID'])
df1.to_csv("tmp2.csv")
</code></pre>

<p>My source files are downloadable from the links below.
<a href="https://drive.google.com/file/d/0BxsAtzIPgj9hUzV0NzV4M1FCQU0/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/0BxsAtzIPgj9hUzV0NzV4M1FCQU0/view?usp=sharing</a>
<a href="https://drive.google.com/file/d/0BxsAtzIPgj9hRmR2MTNQRVdzNUE/view?usp=sharing" rel="nofollow noreferrer">https://drive.google.com/file/d/0BxsAtzIPgj9hRmR2MTNQRVdzNUE/view?usp=sharing</a></p>

<p>P.S. My first post on StackOverflow! Sorry that I'm still learning how to better format it. Thank you for your time and understanding.</p>