如何在数据框的每一行上应用函数? [英] How to apply a function on every row on a dataframe?
问题描述
我是Python的新手,我不确定如何解决以下问题。
我有一个函数:
def EOQ(D,p,ck,ch):
Q = math.sqrt((2 * D * ck)/(ch * p))
return Q
假设我有数据框
df = pd.DataFrame({D:[10,20,30],p:[20,30,10]})
D p
0 10 20
1 20 30
2 30 10
ch = 0.2
ck = 5
和 ch
和 ck
是浮点类型。现在我想将公式应用于数据框的每一行,并将其作为额外的行'Q'返回。一个例子(不起作用)将是:
pre $ $ c $ df ['Q'] = map(lambda p,D:EOQ (D,p,ck,ch),df ['p'],df ['D'])
(仅返回'map'类型)
我需要在我的项目中使用更多类型的处理,我希望找到可行的方法。
因为我不知道 PartMaster
是什么,所以应该如下工作: / b>
def EOQ(D,p,ck,ch):
p,D =部件管理员
Q =数学.sqrt((2 * D * ck)/(ch * p))
返回Q
ch = 0.2
ck = 5
df ['Q'] = df。 apply(lambda row:EOQ(row ['D'],row ['p'],ck,ch),axis = 1)
df
如果你正在计算某个结果的平方根,那么使用 np.sqrt
方法,这是矢量化,并且会显着加快速度:
在[80]中:
df ['Q'] = np.sqrt ((2 * df ['D'] * ck)/(ch * df ['p']))
df
Out [80]:
D p Q
0 10 20 5.000000
1 20 30 5.773503
2 30 10 12.247449
定时
行df:
在[92]中:
导入数学
ch = 0.2
ck = 5
def EOQ(D,p,ck,ch):
Q = math.sqrt((2 * D * ck)/(ch * p))
返回Q
%timeit np.sqrt((2 * df ['D'] * ck)/(ch * df ['p']))
%timeit df.apply lambda行:EOQ(行['D'],行['p'],ck,ch),轴= 1)
1000个循环,最好为3:每循环622μs
1个循环,最好的3:每循环1.19秒
你可以看到np方法快1900倍X
I am new to Python and I am not sure how to solve the following problem.
I have a function:
def EOQ(D,p,ck,ch):
Q = math.sqrt((2*D*ck)/(ch*p))
return Q
Say I have the dataframe
df = pd.DataFrame({"D": [10,20,30], "p": [20, 30, 10]})
D p
0 10 20
1 20 30
2 30 10
ch=0.2
ck=5
And ch
and ck
are float types. Now I want to apply the formula to every row on the dataframe and return it as an extra row 'Q'. An example (that does not work) would be:
df['Q']= map(lambda p, D: EOQ(D,p,ck,ch),df['p'], df['D'])
(returns only 'map' types)
I will need this type of processing more in my project and I hope to find something that works.
As I don't know what PartMaster
is, the following should work:
def EOQ(D,p,ck,ch):
p,D = Partmaster
Q = math.sqrt((2*D*ck)/(ch*p))
return Q
ch=0.2
ck=5
df['Q'] = df.apply(lambda row: EOQ(row['D'], row['p'], ck, ch), axis=1)
df
If all you're doing is calculating the square root of some result then use the np.sqrt
method this is vectorised and will be significantly faster:
In [80]:
df['Q'] = np.sqrt((2*df['D']*ck)/(ch*df['p']))
df
Out[80]:
D p Q
0 10 20 5.000000
1 20 30 5.773503
2 30 10 12.247449
Timings
For a 30k row df:
In [92]:
import math
ch=0.2
ck=5
def EOQ(D,p,ck,ch):
Q = math.sqrt((2*D*ck)/(ch*p))
return Q
%timeit np.sqrt((2*df['D']*ck)/(ch*df['p']))
%timeit df.apply(lambda row: EOQ(row['D'], row['p'], ck, ch), axis=1)
1000 loops, best of 3: 622 µs per loop
1 loops, best of 3: 1.19 s per loop
You can see that the np method is ~1900 X faster
这篇关于如何在数据框的每一行上应用函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!