user3379482
user3379482

Reputation: 567

How to add pandas data frame column based on other rows values

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

Upvotes: 1

Views: 137

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150805

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

Upvotes: 1

Related Questions