在每行的数据框列中应用函数

时间:2017-04-24 17:00:25

标签: python pandas dataframe

我有一个pandas数据框,显示为:

df1['A'].ix[1:3]       Sims          Grade    SleepNumber
2017-01-01 02:00:00    [33, 34, 39]  5        6
2017-01-01 03:00:00    [3, 43, 9]    1        12

我的函数定义为:

def Fn(S, G, SL):
 #some complicated operation but for example it returns only product
 return S*G*SL

我想为df1的每一行执行以下操作,以获取数据帧:

df1['A'].ix[1:3]       FnResult
2017-01-01 02:00:00    [Fn(33, 5, 6), Fn(34, 5, 6), Fn(39, 5, 6)]  
2017-01-01 03:00:00    [Fn(3, 1, 12), Fn(43, 1, 12), Fn(9, 1, 12)]   

我尝试了以下内容:

z1 = df1.apply(map(lambda x:Fn([x, x.Grade, x.SleepNumber]), x.Sims))

但我没有正确设置,所以它出错了。

1 个答案:

答案 0 :(得分:0)

您可能会将 Fn 重写为,因为 S 是一列列表:

def Fn(S, G, SL):
    return [s*G*SL for s in S]

df['FnResult'] = df.apply(lambda r: Fn(r.Sims, r.Grade, r.SleepNumber), axis=1).values.tolist()

df
#                           Sims    Grade   SleepNumber          FnResult
#2017-01-01 02:00:00    [33, 34, 39]    5           6   [990, 1020, 1170]
#2017-01-01 03:00:00      [3, 43, 9]    1           12     [36, 516, 108]

或者不修改 Fn ,修改map方法中的apply函数;回想一下,map本身不可调用,你需要将它包装在另一个 lambda 中:

df['FnResult'] = df.apply(lambda r: list(map(lambda s: Fn(s, r.Grade, r.SleepNumber), r.Sims)), axis=1)

df
#                               Sims    Grade   SleepNumber         FnResult
#2017-01-01 02:00:00    [33, 34, 39]        5           6    [990, 1020, 1170]
#2017-01-01 03:00:00    [3, 43, 9]          1           12      [36, 516, 108]