I have a dataframe which I’d like to adjust the values which are associated with a string from a different column. Example, all values in column ‘wgt’ that is associated with ‘joe’ in the ‘name’ column to multiply 1.10.
original df
╔══════╦═════╗ ║ name ║ wgt ║ ╠══════╬═════╣ ║ joe ║ 10 ║ ║ gary ║ 8 ║ ║ pete ║ 12 ║ ║ pete ║ 13 ║ ║ pete ║ 14 ║ ║ joe ║ 11 ║ ║ gary ║ 7 ║ ║ gary ║ 5 ║ ║ gary ║ 7 ║ ╚══════╩═════╝
adjusted df
╔══════╦═════╗ ║ name ║ wgt ║ ╠══════╬═════╣ ║ joe ║ 11 ║ ║ gary ║ 8 ║ ║ pete ║ 12 ║ ║ pete ║ 13 ║ ║ pete ║ 14 ║ ║ joe ║ 12.1║ ║ gary ║ 7 ║ ║ gary ║ 5 ║ ║ gary ║ 7 ║ ╚══════╩═════╝
code for sample df
import pandas as pd
data = {'name':['joe','gary','pete','pete','pete','joe','gary','gary','gary'
],'wgt':[10,8,12,13,14,11,7,5,7,]}
df = pd.DataFrame(data)
df
thanks in advance
>Solution :
df['wgt'] = np.where(df.name == "joe", df.wgt * 1.1, df.wgt)
or
df.loc[df.name == "joe", "wgt"] *= 1.1