python根据行值添加一列

时间:2018-09-13 05:46:41

标签: python pandas dataframe

我有一个数据框,如下所示。我想基于“需要”列添加一列(例如,在零行中,需要为1,所以选择part1值-0.17。我粘贴了想要的数据框。谢谢。

 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

2 个答案:

答案 0 :(得分:6)

使用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解决方案,就像样本中一样,必须按1对增加的列进行排序:

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

答案 1 :(得分:3)

应该没事

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