我是python和Pandas的新手,我有一个庞大的数据集,并希望逐行应用函数,我想应用于一批行并将结果返回并关联到同一行
示例:
ID Values
a 2
b 3
c 4
d 5
e 6
f 7
df['squared_values']= df['values'].apply(lambda row: function(row))
def function(x):
#making call to api and returning values related to x
return response
逐行应用功能比较耗时
我需要一种对行进行批处理的方法
example:
batch=3
df['squared_values']= df['values'].apply(lambda batch: function(batch))
on first pass values should be
ID Values squared_values
a 2 4
b 3 9
c 4 16
d 5
e 6
f 7
on second pass
ID Values squared_values
a 2 4
b 3 9
c 4 16
d 5 25
e 6 36
f 7 49
答案 0 :(得分:0)
这个操作真的太慢了吗?
df['squared_values'] = df['Values'] ** 2
您始终可以添加iloc来选择行:
df.iloc['squared_values'].update(df.iloc[0:4]['Values'] ** 2)
但是我无法想象这会更快