[英]Applying a function to dataframe with index and columns names as arguments
[英]Applying a function on specific Dataframe rows with datetime index as arguments
给定以下数据框
import pandas as pd
from collections import OrderedDict
d = OrderedDict([ ('Date_Time', ['2016-01-18 00:00:00', '2016-01-18 12:00:00', '2016-01-19 00:00:00', '2016-01-19 12:00:00']),
('Symbol', ['AUD', 'AUD', 'AUD', 'AUD']),
('Hit', [False, False, True, False]),
] )
df = pd.DataFrame.from_dict(d)
df = df.set_index('Date_Time')
print(df)
Symbol Hit
Date_Time
2016-01-18 00:00:00 AUD False
2016-01-18 12:00:00 AUD False
2016-01-19 00:00:00 AUD True
2016-01-19 12:00:00 AUD False
仅当Hit为true时,如何将Date_Time索引和以下内容传递给函数,然后将该同一函数的结果返回到另一列(例如Score )?
在这种特殊情况下,使用伪代码:
my_func(dt1, dt2):
#do something on dt1 and d2
return True
如果要创建新列,可以执行以下操作:
def dosomething(x, y):
return something
df['New Column'] = dosomething(df_col1, df_col2)
这将创建一个新列,其逻辑依赖于这两列。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.