0

Suppose I condition a dataframe the following way:

df[df['category_id'] == 'wall']

this results in:

    xmin    ymin    xmax    ymax    category_id width   height
30  45.0    58.0    45.0    520.0   wall         0.0    462.0
31  45.0    58.0    754.0   58.0    wall         709.0  0.0
32  302.0   58.0    302.0   520.0   wall         0.0    462.0
33  563.0   58.0    563.0   520.0   wall         0.0    462.0
34  626.0   58.0    626.0   257.0   wall         0.0    199.0
35  754.0   58.0    754.0   321.0   wall         0.0    263.0
36  563.0   159.0   754.0   159.0   wall         191.0  0.0

what I would like to do is update width and height by a constant if either one of them is 0.

I think I could use iterrows() here, but is there some more elegant way to go about this?

Nate
  • 193
  • 5
  • Have you looked at `df.replace` or mask where values which are 0 in `width` and use `fillna` with constant value and assign them back. – Ch3steR Aug 19 '20 at 07:24
  • Only the one equaling 0. – Nate Aug 19 '20 at 07:29
  • `df.loc[df['category_id'].eq('wall'), ['width', 'height']] = df.loc[df['category_id'].eq('wall'), ['width', 'height']].replace(0, const_value)` – Ch3steR Aug 19 '20 at 07:33

0 Answers0