1

I have a data frame like

    x           y           w           h
0   1593.826218 1293.189452 353.268389  74.493565
1   1680.089430 1956.536916 87.632469   42.567752
2   1362.421731 1908.648195 52.031778   42.567752
3   1599.303248 1385.419580 351.899131  78.040878
4   1500.716721 1121.144789 397.084623  46.115064
5   1513.040037 1186.770072 514.840753  86.909160
6   1387.068363 1804.002472 212.234885  44.341408
7   787.333657  379.756446  416.254225  70.946253

I want to select rows based on certain value ranges in x and y and find the values in all four x,y,w,h and perform addition or subtraction on those values and replace them with the calculated value in that row.

I am doing something like

df.loc[(df['x'] >= 1000) & (df['x'] < 1800) & (df['y'] >= 1150)  & (df['y'] < 1290), ['x', 'y', 'w','h']] =  df['x'] - 20, df['y'] - 165, df['w'] + 26, df['h'] - 29

and getting error: "Must have equal len keys and value when setting with an ndarray"

when I tried this

df.loc[(df['x'] >= 1000) & (df['x'] < 1800) & (df['y'] >= 1150)  & (df['y'] < 1290), 'x'] =  df['x'] - 20

it works but I want to perform operation on all four columns in one go and update the values.

My desired answer is it should select row 5 and my answer should be like

    x           y           w           h
5   1493.040037 1021.770072 540.840753  57.909160

Any help will be much appreciated.

3 Answers 3

5

Let us fix your code

m = (df['x'] >= 1000) & (df['x'] < 1800) \
        & (df['y'] >= 1150)  & (df['y'] < 1290)

df.loc[m] += [-20, -165, 26, -29]

             x            y           w          h
0  1593.826218  1293.189452  353.268389  74.493565
1  1680.089430  1956.536916   87.632469  42.567752
2  1362.421731  1908.648195   52.031778  42.567752
3  1599.303248  1385.419580  351.899131  78.040878
4  1500.716721  1121.144789  397.084623  46.115064
5  1493.040037  1021.770072  540.840753  57.909160 *** updated
6  1387.068363  1804.002472  212.234885  44.341408
7   787.333657   379.756446  416.254225  70.946253
Sign up to request clarification or add additional context in comments.

Comments

2

With your approach , you can use pd.concat on the R.H.S

df.loc[(df['x'] >= 1000) & (df['x'] < 1800) & (df['y'] >= 1150)  & (df['y'] < 1290), ['x', 'y', 'w','h']]=pd.concat((df['x'] - 20, df['y'] - 165, df['w'] + 26, df['h'] - 29),axis=1)

             x            y           w          h
0  1593.826218  1293.189452  353.268389  74.493565
1  1680.089430  1956.536916   87.632469  42.567752
2  1362.421731  1908.648195   52.031778  42.567752
3  1599.303248  1385.419580  351.899131  78.040878
4  1500.716721  1121.144789  397.084623  46.115064
5  1493.040037  1021.770072  540.840753  57.909160
6  1387.068363  1804.002472  212.234885  44.341408
7   787.333657   379.756446  416.254225  70.946253

Comments

1

You have to assign with an array of the same shape. Easiest way is to use the original df:

m = (df['x'] >= 1000) & (df['x'] < 1800) & (df['y'] >= 1150) & (df['y'] < 1290)

df.loc[m] = df.assign(x=df["x"]-20, y=df["y"]-165, w=df['w']+26, h=df['h']-29)

print (df[m])

             x            y           w         h
5  1493.040037  1021.770072  540.840753  57.90916

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.