Python: Replace df1.col values with df2.col2 values
<p>I have two data frames df1 and df2. 
In df1 I have 50 columns and in df2 I have 50+ columns. What I want to achieve is that 
In df1 I have 13000 rows and a column name subject where names of all subjects are given.
In df2 I have 250 rows and along 50+ I have two columns named subject code and subject_name. </p>

<pre><code>        Here is an example of my datasets:

        df1 = 
        index     subjects
        0         Biology
        1         Physicss
        2         Chemistry
        3         Biology
        4         Physics
        5         Physics
        6         Biolgy

    df2 = 
        index     subject_name    subject_code
        0         Biology         BIO
        1         Physics         PHY
        2         Chemistry       CHE
        3         Medical         MED
        4         Programming     PRO
        5         Maths           MAT
        6         Literature      LIT 

My desired output in df1 (after replacing subject_name and fixing the spelling errors) is:
            index     subjects        subject_code
            0         Biology         BIO
            1         Physics         PHY
            2         Chemistry       CHE
            3         Biology         BIO
            4         Physics         PHY
            5         Physics         PHY
            6         Biology         BIO
</code></pre>

<p>What happens at my end is that I want to merge all subject values in df1 with values in df2 subject name values. In df1 there are around 500 rows where I get NAN after I merge both the columns in one as in these 500 rows there is some difference in the spellings of the subject. 
 I have tried solution given at the following links but didn't work for me:
<a href="http://stackoverflow.com/questions/34946913/replace-df-index-values-with-values-from-a-list-but-ignore-empty-strings">replace df index values with values from a list but ignore empty strings</a></p>

<p><a href="http://stackoverflow.com/questions/31751230/python-pandas-replace-values-multiple-columns-matching-multiple-columns-from-an">Python pandas: replace values multiple columns matching multiple columns from another dataframe</a></p>

<pre><code>            Here is my code:

            df_merged = pd.merge(df1_subject,df2_subjectname, left_on='subjects', right_on='subject_name')
        df_merged.head()
</code></pre>

<p>Can anyone tell me how I can fix this issue as I have already spend 8 hours on this issue but am unable tor resolve it.</p>

<p>Cheers</p>

<p>One of the issues you have is the incorrect spellings. You can try to harmonise the spelling of the subject across both <code>dataframes</code> using the <code>difflib</code> module and its <code>get_close_matches</code> method. </p>

<p>Using this code will return the closest matching subject for each match in <code>df1</code> and and <code>df2</code>. <code>df1's</code> columns will be updated to reflect this. Therefore, even if the subject name is spelled incorrectly, it will now have the same spelling in both <code>dataframes</code>.</p>

<pre><code>import pandas as pd
import difflib

df2['subject_name'] = df2.subject_name.map(lambda x: difflib.get_close_matches(x, df1.subject)[0])
</code></pre>

<p>After this you can try to merge. It may resolve your issue, but it would be easier to fix if you provide a reproducible example. </p>

python pandas