我正在学习python,并提出了一种逐行计算值的方法,但是我确信有一个更优雅(更快速)的解决方案。这是简单的示例:
df = pd.DataFrame(np.random.rand(10,3), columns=list('abc'))
df.head()
a b c
0 0.207455 0.257266 0.453369
1 0.518193 0.816898 0.141986
2 0.430085 0.490554 0.797655
3 0.369860 0.251664 0.777059
4 0.390059 0.983218 0.966202
df['d']=''
df['e']=''
for i in range(1,len(df)):
df['d'][i]= sqrt((df['a'][i]-df['b'][i])**2+(df['a'][i-1]-df['b'][i-1])**2)
df['e'][i]= (df['c'][i]-df['c'][i-1])*1609
df.head()
a b c d e
0 0.207455 0.257266 0.453369
1 0.518193 0.816898 0.141986 0.30283 -501.015
2 0.430085 0.490554 0.797655 0.304765 1054.97
3 0.369860 0.251664 0.777059 0.132766 -33.1396
4 0.390059 0.983218 0.966202 0.60482 304.331
有更好的方法吗?我正在处理一些大型数据集,以这种方式运行它需要一段时间。
答案 0 :(得分:0)
是的,我们有shift
和diff
,没有for循环
df['d'] = ((df['a'] - df['b']) ** 2 + (df['a'].shift() - df['b'].shift()) ** 2)**0.5
df['e'] = (df['c'].diff()) * 1609
df
a b c d e
0 0.207455 0.257266 0.453369 NaN NaN
1 0.518193 0.816898 0.141986 0.302830 -501.015247
2 0.430085 0.490554 0.797655 0.304764 1054.971421
3 0.369860 0.251664 0.777059 0.132766 -33.138964
4 0.390059 0.983218 0.966202 0.604821 304.331087