简体   繁体   English

Python熊猫在索引上逐行使用lambda

[英]Python Pandas using lambda on index row by row

So I'm trying to apply a function to the index row by row but having some problems 所以我试图逐行将一个函数应用于索引,但是有一些问题

startDate = '2015-05-01 00:00'
endDate = '2015-05-08 00:00'
idx = pd.date_range(startDate, endDate, freq="1min")
df = pd.DataFrame(columns=['F(t)'])
df = df.reindex(idx, fill_value=0)

def circadian_function(T):
    return math.cos(math.pi*(T-delta)/12)

Everything is okay up to here but trying to apply the function I'm not sure what to do 到这里一切都很好,但是尝试应用该功能我不确定该怎么做

df['F(t)'] = df.index.apply(lambda x: circadian_function x[index].hour, axis=1)

Should I be using a lambda? 我应该使用lambda吗? Or just an apply? 还是只是申请?

I don't have 50 rep so I can't comment on @Ted Petrou's answer ;-; 我没有50位代表,所以我无法评论@Ted Petrou的答案; I just wanted to say a couple things that you should know. 我只是想说一些您应该知道的事情。

  1. If you are going to feed df.index.hour into your carcadian_function, make sure you use numpy instead of math . 如果要将df.index.hour输入到carcadian_function中,请确保使用numpy而不是math Otherwise the interpreter will throw a TypeError (I just found out about this). 否则,解释器将抛出TypeError (我刚刚发现了这个错误)。

  2. Make sure to define delta . 确保定义delta

Example: 例:

import numpy as np

def circadian_function(T, delta):
    return np.cos(np.pi * (T-delta) / 12)

What @Ted Petrou recommends you do in full: @Ted Petrou建议您全面执行的操作:

df['F(x)'] = circadian_function(df.index.hour, 0.5) #I picked an arbitrary delta

Numpy will automatically vectorize the function for you. Numpy将自动为您向量化该功能。 Props to Ted I learned something new as well :> 给Ted的道具我也学到了一些新东西:>

Use apply only as a last result. 使用“应用”仅作为最后结果。 This can be easily vectorized. 这可以很容易地向量化。 Make sure you define delta. 确保定义增量。

 import numpy as np
 df['F(t)'] = np.cos(np.pi*(idx.hour-delta)/12)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM