Pandas 对列应用函数

Pandas 对列应用函数,pandas,Pandas,为什么熊猫在尝试将此函数应用于列时抛出错误 import pandas as pd import math data = [ ['LAT', "LON"], [49.00, -83.04], [41.00, -83.04], [26.00, -83.04], ] df= pd.DataFrame(data[1:], columns=data[0]) print(df) print((math.cos(49.00) * 69.172) /.25) df['L

为什么熊猫在尝试将此函数应用于列时抛出错误

import pandas as pd
import math

data = [
    ['LAT', "LON"],
    [49.00, -83.04],
    [41.00, -83.04],
    [26.00, -83.04],
]
df= pd.DataFrame(data[1:], columns=data[0])

print(df)
print((math.cos(49.00) * 69.172) /.25)


df['LAT'] = df['LAT'].astype(int)
df['test'] = df.apply(lambda t: ((math.cos(t['LAT']) * 69.172) /.25))
尝试使用df.apply时,最后一行出现错误消息。输出为:

    LAT    LON
0  49.0 -83.04
1  41.0 -83.04
2  26.0 -83.04
83.17034974333946

  File "pandas/_libs/index.pyx", line 154, in pandas._libs.index.IndexEngine.get_loc
  File "pandas/_libs/hashtable_class_helper.pxi", line 759, in pandas._libs.hashtable.Int64HashTable.get_item
TypeError: an integer is required
我认为需要:

但更好的方法是使用矢量化:


是的,这很有效,但我不知道为什么哈哈,这就像是一样的。谢谢
df['LAT']
不需要
.astype(int)
对,这是一个调试步骤。我猜整数的引用是关于索引的。
df['test'] = df['LAT'].apply(lambda t: ((math.cos(t) * 69.172) /.25))
print (df)
    LAT    LON        test
0  49.0 -83.04   83.170350
1  41.0 -83.04 -273.184930
2  26.0 -83.04  178.994813
df['test'] = np.cos(df['LAT']) * 69.172 / .25