Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to multiply specific pandas column with matching dictionary key/value pair in pandas

I have a dataframe with specific headers and a dictionary with matching headers. I wish to simply multiply the value of the given matching key with that matching column in pandas. Here is the pandas DF

           ID  208230_s_at norm  242009_at norm  211535_s_at norm  ...   AffyVisit  Gender(M/F)  DxCode    dx
0  phchp083v4          0.005718    6.641902e-04          0.008335  ...  phchp083v4            M      SZ  M-SZ
1  phchp090v1          0.032542    4.315564e-04          0.011494  ...  phchp090v1            M      SZ  M-SZ
2  phchp090v2          0.021074    6.180979e-04          0.006773  ...  phchp090v2            M      SZ  M-SZ
3  phchp090v3          0.015779    4.156884e-04          0.009435  ...  phchp090v3            M      SZ  M-SZ
4  phchp133v2          0.016416    2.205289e-27          0.005122  ...  phchp133v2            M      SZ  M-SZ
5  phchp308v5          0.002679    2.964330e-04          0.006225  ...  phchp308v5            M      SZ  M-SZ
6  phchp366v2          0.009805    8.229048e-04          0.014914  ...  phchp366v2            M      SZ  M-SZ
7  phchp375v1          0.023069    2.961317e-04          0.010417  ...  phchp375v1            M      SZ  M-SZ
8  phchp375v2          0.020838    3.398511e-04          0.010711  ...  phchp375v2            M      SZ  M-SZ

and here is my dictionary with headers that match the above DF with a given value:

{'208230_s_at norm': 2.3012184212079356, '242009_at norm': 14.965237539220922, '211535_s_at norm': 5.499552595639465, '204183_s_at norm': 3.0375335449229115, '224818_at norm': 3.4170449181626052, '201668_x_at norm': 0.4067131613648669, '206385_s_at norm': 4.737388624118393, '210924_at norm': 1632.4015971405947}

I just wish to multiply column 208230_s_at norm with the same dictionary key, by that keys value. Thank you.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

dict.items() can get you a list of the keys and the values from a dictionary. Try this:

for key, value in dict.items(): 
    df[key] = df[key] * value
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading