[Python] replace values of a column (but keep NA values) with list of other values python

Discussão em 'Python' iniciado por Stack, Dezembro 18, 2024.

  1. Stack

    Stack Membro Participativo

    I would like to start since I am looking for tips on how to tackle this issue, rather than specific code with an answer I have not provided any code, just my situation. Is this allowed? Or am I still required to provide some form of code? I am unsure on these guidelines so if this is not allowed please inform me and I will not do this next time :)

    I have the following dataframe:

    well​
    sample
    A1​
    1
    NaN​
    2
    NaN​
    3
    A2​
    1
    NaN​
    2
    A3​
    1
    NaN​
    2
    NaN​
    3​

    Is it possible to replace the values in the column 'well' (thus the A1, A2, A3 etc) with the values of a list? I have made a list = [0,31,20] for example and need the well names replaced by this list or I need an extra column with the values of the list on the same row as the well name (thus other rows in the column need to stay NaN)

    I have tried working with for example df.replace() where I tried using a dictionary in stead of list, or using the list but these approaches did not work. I hope to obtain tips on how to tackle this issue (thus not specific codes with the answer, I am still learning python therefore tips could help me in the learning process rather than specific pieces of code with a solution)

    To visualise below the two options I am searching for as final dataframe:

    Option 1

    well​
    sample
    0​
    1
    NaN​
    2
    NaN​
    3
    31​
    1
    NaN​
    2
    20​
    1
    NaN​
    2
    NaN​
    3​

    Or option 2:

    well​
    sample​
    Position
    A1​
    1​
    0
    NaN​
    2​
    NaN
    NaN​
    3​
    NaN
    A2​
    1​
    31
    NaN​
    2​
    NaN
    A3​
    1​
    20
    NaN​
    2​
    NaN
    NaN​
    3​
    NaN​

    Thank you in advance for your help!

    Continue reading...

Compartilhe esta Página