Patthebug Patthebug - 8 days ago 5x
Python Question

Replacing values in a column for a subset of rows

I have a

having multiple columns. I would like to replace the value in a column called
. Now this value needs to only be replaced for a few rows, whenever a condition is met in another column called
. I tried various methods; The most common method seems to be using the
method, but for some reason it doesn't work for me.

Here are the variations that I am unsuccessfully trying:

- variable used for condition checking

- variable used for subsetting the
(starts from zero)

Variation 1:

df[df.ids == encodedid].loc[df.ids==encodedid, 'Discriminant'].values[indices] = 'Y'

Variation 2:

df[df['ids'] == encodedid].iloc[indices,:].set_value('questionid','Discriminant', 'Y')

Variation 3:

df.loc[df.ids==encodedid, 'Discriminant'][indices] = 'Y'

Variation 3
particularly has been disappointing in that most posts on SO tend to say it should work but it gives me the following error:

ValueError: [ 0 1 2 3 5 6 7 8 10 11 12 13 14 16 17 18 19 20 21 22 23] not contained in the index

Any pointers will be highly appreciated.


you are slicing too much. try something like this:

indexer = df[df.ids == encodedid].index
df.loc[indexer, 'Discriminant'] = 'Y'

.loc[] needs an index list and a column list. you can set the value of that slice easily using = 'what you need'

looking at your problem you might want to set that for 2 columns at the same time such has:

indexer = df[df.ids == encodedid].index
column_list = ['Discriminant', 'questionid']

df.loc[indexer, column_list] = 'Y'