1

I am trying to add a new column and set its value based on other rows values. Lets say we have the following data frame:

    df = pd.DataFrame({
         'B':[1,2,3,4,5,6],
         'C':[7,8,9,4,2,3],
         'D':[1,3,5,7,1,0],
    })
    

With this corresponding output

    B   C   D
    1   7   1
    2   8   3
    3   9   5
    4   4   7
    5   2   1
    6   3   0

I want to add a new column 'E', which has the following value: E = df.C value where B = B + 2.
For example, the first value of E should be 3 (we select the row where B = 0+2 = 2, and select C value from that row). I tried the following

    f['E'] = np.where(f.B == (f['B']+2))['C']

But it's not working

0

1 Answer 1

1

You can set B and index and use that to map the modified data:

df['E'] = df['B'].add(2).map(df.set_index('B')['C'])

Output:

   B  C  D    E
0  0  1  7  3.0
1  1  2  8  4.0
2  2  3  9  5.0
3  3  4  4  6.0
4  4  5  2  NaN
5  5  6  3  NaN
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, never thought of that!
Is it possible to add a condition? So it becomes: where B = B + 2 and D=value?
@user3379482 maybe df[df['D']==value].set_index('B')['C']?

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.