Skip to content Skip to sidebar Skip to footer

Replace Single Value In A Pandas Dataframe, When Index Is Not Known And Values In Column Are Unique

There is a question on SO with the title 'Set value for particular cell in pandas DataFrame', but it assumes the row index is known. How do I change a value, if the row values in t

Solution 1:

Regardless of the performance, you should be able to do this using loc with boolean indexing:

df = pd.DataFrame([[5, 2], [3, 4]], columns=('a', 'b'))

# modify value in column b where a is 3
df.loc[df.a == 3, 'b'] = 6

df
#   a   b
#0  5   2
#1  3   6

Post a Comment for "Replace Single Value In A Pandas Dataframe, When Index Is Not Known And Values In Column Are Unique"