Replacing values on a dataframe row using a specific value as reference

43 views Asked by At

I have a dataframe like this:

       solution_id   0   1   2   3
0            26688 NaN NaN NaN NaN
1            26689 NaN NaN NaN NaN
2            26690 NaN NaN NaN NaN
3            26691 NaN NaN NaN NaN
4            26692 NaN NaN NaN NaN
...            ...  ..  ..  ..  ..
10398        37086 NaN NaN NaN NaN
10399        37087 NaN NaN NaN NaN
10400        37088 NaN NaN NaN NaN
10401        37089 NaN NaN NaN NaN
10402        37090 NaN NaN NaN NaN

[10403 rows x 5 columns]

I'm going to receive a solution_id and a list of 4 values (let's say [True, False, False, True]). What I need to do is ind the row with the correspondent solution_id and replace the following columns (0, 1, 2, 3) with the list.

I have tried using something like:

filter_ = (df['solution_id'] == solution_id)
df.loc[filter_] = [solution_id] + results

Or even:

idx = df['solution_id'].loc[df['solution_id'] == solution_id].index[0]
df.loc[idx] = [solution_id] + results

But both don't work and I'm not sure why. The first runs but doesn't register anything and the second one says the index is empty, so I'm assuming it is not finding anything with the filter. Problem is, I know for sure that every solution_id is in there. So I don't know what to do.

1

There are 1 answers

1
ASW22 On BEST ANSWER

Something like Pandas update multiple columns at once

list1 = [True, False, False, True]
solution_id = 26690
df.loc[df["solution_id"] == solution_id, [0,1,2,3]] = list1