1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Pandas - Change specific input based on row and column name

Discussão em 'Python' iniciado por Stack, Setembro 9, 2025.

  1. Stack

    Stack Membro Participativo

    Lets say I have the following dataframe

    data = [['Tom', 180], ['Adam', 174], ['Bob', 182]]
    df = pd.DataFrame(data, columns=['Name', 'Height'])


    If I wish to change the value of Bob's height, I could use .at as follows: df.at[2, 'Height'] = 190. However since every name is unique, Im looking for a way to do something along the line of df.at['Bob', 'Height'] = 190. After tinkering with I bit, I ended up opting to first find the location of Bob within df and then use its location along side .at

    location = np.where(df['Name'].values == 'Bob')[0][0]
    df.at[location, 'Height'] = 190


    However, There must be a better way to do this. Note that I am not adamant on the use of .at property. I think my solution is reasonable, but I want to know if there is something better out there.

    After playing with it some more I found this

    df.loc[df['Name'] == 'Bob', 'Height'] = 190


    I am quite happy with this. Clearly this method would cause issues if I had repeats. But fortunately I do not.

    Continue reading...

Compartilhe esta Página