用结果替换列中的值

时间:2019-04-09 15:23:20

标签: python pandas

我想用列中值的结果替换数据集中列中的值。示例:我的数据集有

id          tslot         A
1   2014-11-02 22:45:00   89    
1   2014-10-26 09:15:00   762
1   2014-10-26 11:00:00   25
1   2014-10-26 11:15:00   762
1   2014-10-26 12:00:00   25    
1   2014-10-26 13:00:00   25
1   2014-10-26 13:45:00   25    
1   2014-10-26 14:45:00   902
1   2014-10-26 15:45:00   902

运行某些函数后,我获得了A列的一些值。

          Emb_A
762.0   -0.237305
89.0    -0.033929
902.0   -0.237330
25.0    -0.237300
1176.0  -0.237300
827.0   -0.237312
1227.0  -0.237301

因此,我想为数据集再添加一列,并将Emb_A值放在该列中。

我的预期输出是:

id        tslot            A        Emb_A
1   2014-11-02 22:45:00   89    -0.033929
1   2014-10-26 09:15:00   762   -0.237305
1   2014-10-26 11:00:00   25    -0.237300
1   2014-10-26 11:15:00   762   -0.237305
1   2014-10-26 12:00:00   25    -0.237300
1   2014-10-26 13:00:00   25    -0.237300
1   2014-10-26 13:45:00   25    -0.237300
1   2014-10-26 14:45:00   902   -0.237330
1   2014-10-26 15:45:00   902   -0.237330

2 个答案:

答案 0 :(得分:0)

定义要在列A上运行的功能

然后使用apply方法通过lambda函数将其应用于整个列。

示例:

def your_function(x):
    # x is individual value from desired column
    # operate on x here. For eg. square(x)
    return x**2

df["Emb_A"] = df["A"].apply(lambda x: your_function(x))

答案 1 :(得分:0)

您可以使用:

df = pd.DataFrame({'A': [89,762,25,762,25,25,25,902,902]})
df1 = pd.DataFrame({'Emb_A': [-0.237305,-0.033929,-0.237330,-0.237300, -0.237300,-0.237312,-0.237301]}, index=[762.0,89.0,902.0,25.0,1176.0,827.0,1227.0])
df1.reset_index(inplace=True)
df['Emb_A'] = df['A'].apply(lambda x: float(df1[df1['index']==float(x)]['Emb_A']))
print(df)

     A     Emb_A
0   89 -0.033929
1  762 -0.237305
2   25 -0.237300
3  762 -0.237305
4   25 -0.237300
5   25 -0.237300
6   25 -0.237300
7  902 -0.237330
8  902 -0.237330

更新

df = df.merge(df1, left_on='A', right_on='index')
df.drop('index', axis=1, inplace=True)
     A     Emb_A
0   89 -0.033929
1  762 -0.237305
2  762 -0.237305
3   25 -0.237300
4   25 -0.237300
5   25 -0.237300
6   25 -0.237300
7  902 -0.237330
8  902 -0.237330