Python 如何计算具有多个列作为参数的列?
我使用了lon和lat组件的风速计算功能:Python 如何计算具有多个列作为参数的列?,python,pandas,Python,Pandas,我使用了lon和lat组件的风速计算功能: def wind_speed(u, v): return np.sqrt(u ** 2 + v ** 2) 并调用它从两个现有列计算一个新列: df['wspeed'] = map(wind_speed, df['lonwind'], df['latwind']) 由于我从Python2.7改为Python3.5,该函数不再工作。变化可能是原因吗 在单参数(列)函数中: def celsius(T): return round(T
def wind_speed(u, v):
return np.sqrt(u ** 2 + v ** 2)
并调用它从两个现有列计算一个新列:
df['wspeed'] = map(wind_speed, df['lonwind'], df['latwind'])
由于我从Python2.7改为Python3.5,该函数不再工作。变化可能是原因吗
在单参数(列)函数中:
def celsius(T):
return round(T - 273, 1)
def celsius(T):
# using vectorized function: np.round()
return np.round(T - 273, 1)
我现在使用:
df['temp'] = df['t2m'].map(celsius)
而且效果很好
您能帮助我吗?我会尽量坚持使用现有的numpy/scipy函数,因为它们非常快速且经过优化(): 计时:针对300K行DF:
In [47]: df = pd.concat([df] * 10**5, ignore_index=True)
In [48]: df.shape
Out[48]: (300000, 2)
In [49]: %paste
def wind_speed(u, v):
return np.sqrt(u ** 2 + v ** 2)
## -- End pasted text --
In [50]: %timeit list(map(wind_speed, df['lonwind'], df['latwind']))
1 loop, best of 3: 922 ms per loop
In [51]: %timeit np.hypot(df.latwind, df.lonwind)
100 loops, best of 3: 4.08 ms per loop
结论:矢量化方法速度快230倍
如果您必须自己编写一个,请尝试使用向量化数学(使用向量/列而不是标量):
演示:
与celsius()
函数相同的矢量化方法:
def celsius(T):
return round(T - 273, 1)
def celsius(T):
# using vectorized function: np.round()
return np.round(T - 273, 1)
如果要使用,请添加列表
:
df = pd.DataFrame({'lonwind':[1,2,3],
'latwind':[4,5,6]})
print (df)
latwind lonwind
0 4 1
1 5 2
2 6 3
def wind_speed(u, v):
return np.sqrt(u ** 2 + v ** 2)
df['wspeed'] = list(map(wind_speed, df['lonwind'], df['latwind']))
print (df)
latwind lonwind wspeed
0 4 1 4.123106
1 5 2 5.385165
2 6 3 6.708204
df['wspeed'] = (map(wind_speed, df['lonwind'], df['latwind']))
print (df)
latwind lonwind wspeed
0 4 1 <map object at 0x000000000AC42DA0>
1 5 2 <map object at 0x000000000AC42DA0>
2 6 3 <map object at 0x000000000AC42DA0>
无列表
:
df = pd.DataFrame({'lonwind':[1,2,3],
'latwind':[4,5,6]})
print (df)
latwind lonwind
0 4 1
1 5 2
2 6 3
def wind_speed(u, v):
return np.sqrt(u ** 2 + v ** 2)
df['wspeed'] = list(map(wind_speed, df['lonwind'], df['latwind']))
print (df)
latwind lonwind wspeed
0 4 1 4.123106
1 5 2 5.385165
2 6 3 6.708204
df['wspeed'] = (map(wind_speed, df['lonwind'], df['latwind']))
print (df)
latwind lonwind wspeed
0 4 1 <map object at 0x000000000AC42DA0>
1 5 2 <map object at 0x000000000AC42DA0>
2 6 3 <map object at 0x000000000AC42DA0>
但是功能
map
是否已更改?