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