Hong
Hong

Reputation: 263

python add one column based on row value

I have one dataframe as below. I want to add one column based on the column 'need' (such as in row zero ,the need is 1, so select part1 value -0.17. I have pasted the dataframe which I want. Thanks.

 df = pd.DataFrame({
        'date': [20130101,20130101, 20130103, 20130104, 20130105, 20130107],
         'need':[1,3,2,4,3,1],
        'part1':[-0.17,-1.03,1.59,-0.05,-0.1,0.9],
         'part2':[0.67,-0.03,1.95,-3.25,-0.3,0.6],
         'part3':[0.7,-3,1.5,-0.25,-0.37,0.62],
         'part4':[0.24,-0.44,1.335,-0.45,-0.57,0.92]
    }) 
           date  need  output  part1  part2  part3  part4
    0  20130101     1   -0.17  -0.17   0.67   0.70  0.240
    1  20130101     3   -3.00  -1.03  -0.03  -3.00 -0.440
    2  20130103     2    1.95   1.59   1.95   1.50  1.335
    3  20130104     4   -0.45  -0.05  -3.25  -0.25 -0.450
    4  20130105     3   -0.37  -0.10  -0.30  -0.37 -0.570
    5  20130107     1    0.90   0.90   0.60   0.62  0.920

Upvotes: 2

Views: 93

Answers (2)

SamFH
SamFH

Reputation: 31

It should be fine

df['new'] = df.iloc[:, 1:].apply(lambda row: row['part'+str(int(row['need']))], axis=1)

       date  need  part1  part2  part3  part4   new
0  20130101     1  -0.17   0.67   0.70  0.240   -0.17
1  20130101     3  -1.03  -0.03  -3.00 -0.440   -3.00
2  20130103     2   1.59   1.95   1.50  1.335    1.95
3  20130104     4  -0.05  -3.25  -0.25 -0.450   -0.45
4  20130105     3  -0.10  -0.30  -0.37 -0.570   -0.37
5  20130107     1   0.90   0.60   0.62  0.920    0.90

Upvotes: 3

jezrael
jezrael

Reputation: 862511

Use DataFrame.lookup:

df['new'] = df.lookup(df.index, 'part' + df['need'].astype(str))
print (df)
       date  need  part1  part2  part3  part4   new
0  20130101     1  -0.17   0.67   0.70  0.240 -0.17
1  20130101     3  -1.03  -0.03  -3.00 -0.440 -3.00
2  20130103     2   1.59   1.95   1.50  1.335  1.95
3  20130104     4  -0.05  -3.25  -0.25 -0.450 -0.45
4  20130105     3  -0.10  -0.30  -0.37 -0.570 -0.37
5  20130107     1   0.90   0.60   0.62  0.920  0.90

Numpy solution, is necessary sorting increaseing columns by 1 like in sample:

df['new'] = df.filter(like='part').values[np.arange(len(df)), df['need'] - 1]
print (df)
       date  need  part1  part2  part3  part4   new
0  20130101     1  -0.17   0.67   0.70  0.240 -0.17
1  20130101     3  -1.03  -0.03  -3.00 -0.440 -3.00
2  20130103     2   1.59   1.95   1.50  1.335  1.95
3  20130104     4  -0.05  -3.25  -0.25 -0.450 -0.45
4  20130105     3  -0.10  -0.30  -0.37 -0.570 -0.37
5  20130107     1   0.90   0.60   0.62  0.920  0.90

Upvotes: 6

Related Questions