0

I have a dataframe (df1) that I want to replace the values in the symtom_1, symptom_2... with the weight values from the df2 dataframe.

The first dataframe has 4000 rows and 17 columns

df1

   Disease            Symptom_1    Symptom_2    Symptom_3
0  Fungal infection   itching      itching      NaN
1  Fungal infection   skin_rash    itching      NaN
2  Fungal infection   itching      itching      NaN
3  Fungal infection   itching      itching      skin_rash
4  vertigo            itching      skin_rash    skin_rash
5  vertigo            vomiting     skin_rash    vomiting
6  vertigo            vomiting     skin_rash    vomiting
7  vertigo            vomiting     vomiting     skin_rash
8  Fungal infection   vomiting     vomiting     vomiting
9  Fungal infection   skin_rash    skin_rash    vomiting
10 Fungal infection   skin_rash    vomiting     itching

The second dataframe has 133 rows

df2

    Symptom              weight
0   itching                 1
1   skin_rash               3
2   nodal_skin_eruptions    4
3   continuous_sneezing     4
4   shivering               5

4
  • 1
    Does this answer your question? Replace rows in one dataframe with another Commented Mar 16, 2021 at 23:16
  • Stack Overflow has wealth of information on how to replace dataframes with another. Please research the link i shared above Commented Mar 16, 2021 at 23:17
  • or this one stackoverflow.com/questions/63418515/… Commented Mar 16, 2021 at 23:20
  • I don't think either of those dupes directly answer OP question. merging would first require melting the dataframe. Commented Mar 16, 2021 at 23:24

1 Answer 1

1

you can use replace and pass in a dictionary.

repl_dict = df2.set_index('Symptom')['weight'].to_dict()

print(df1.replace(repl_dict))

             Disease Symptom_1 Symptom_2 Symptom_3
0   Fungal infection         1         1       NaN
1   Fungal infection         3         1       NaN
2   Fungal infection         1         1       NaN
3   Fungal infection         1         1         3
4            vertigo         1         3         3
5            vertigo  vomiting         3  vomiting
6            vertigo  vomiting         3  vomiting
7            vertigo  vomiting  vomiting         3
8   Fungal infection  vomiting  vomiting  vomiting
9   Fungal infection         3         3  vomiting
10  Fungal infection         3  vomiting         1

repl_dict

{'itching': 1,
 'skin_rash': 3,
 'nodal_skin_eruptions': 4,
 'continuous_sneezing': 4,
 'shivering': 5}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.