我有一个OHLC价格的数据集,我从CSV解析成熊猫数据帧和重采样到15个分钟吧:
<class 'pandas.core.frame.DataFrame'>
DatetimeIndex: 500047 entries, 1998-05-04 04:45:00 to 2012-08-07 00:15:00
Freq: 15T
Data columns:
Close 363152 non-null values
High 363152 non-null values
Low 363152 non-null values
Open 363152 non-null values
dtypes: float64(4)
Run Code Online (Sandbox Code Playgroud)
我想添加各种计算列,从简单的列开始,例如期间范围(HL),然后是布尔值,以指示我将定义的价格模式的出现 - 例如锤子蜡烛模式,其样本定义:
def closed_in_top_half_of_range(h,l,c):
return c > l + (h-l)/2
def lower_wick(o,l,c):
return min(o,c)-l
def real_body(o,c):
return abs(c-o)
def lower_wick_at_least_twice_real_body(o,l,c):
return lower_wick(o,l,c) >= 2 * real_body(o,c)
def is_hammer(row):
return lower_wick_at_least_twice_real_body(row["Open"],row["Low"],row["Close"]) \
and closed_in_top_half_of_range(row["High"],row["Low"],row["Close"])
Run Code Online (Sandbox Code Playgroud)
基本问题:如何将函数映射到列,特别是我想引用多个其他列或整行或其他什么?
这篇文章涉及从单个源列添加两个计算列,这些列很接近但不完全相同.
稍微高级一点:对于参考多个条形(T)确定的价格模式,我如何从函数定义中引用不同的行(例如T-1,T-2等)?
我有两个DataFrames,我想根据列合并.然而,由于交替拼写,不同数量的空格,不存在/存在变音符号,我希望能够合并,只要它们彼此相似即可.
任何相似性算法都可以(soundex,Levenshtein,difflib).
假设一个DataFrame具有以下数据:
df1 = DataFrame([[1],[2],[3],[4],[5]], index=['one','two','three','four','five'], columns=['number'])
number
one 1
two 2
three 3
four 4
five 5
df2 = DataFrame([['a'],['b'],['c'],['d'],['e']], index=['one','too','three','fours','five'], columns=['letter'])
letter
one a
too b
three c
fours d
five e
Run Code Online (Sandbox Code Playgroud)
然后我想得到生成的DataFrame
number letter
one 1 a
two 2 b
three 3 c
four 4 d
five 5 e
Run Code Online (Sandbox Code Playgroud) 我有一个由两列字符串组成的pandas DataFrame.我想创建一个包含两列的编辑距离的第三列.
from nltk.metrics import edit_distance
df['edit'] = edit_distance(df['column1'], df['column2'])
Run Code Online (Sandbox Code Playgroud)
出于某种原因,这似乎是某种无限循环,因为它在相当长的一段时间内仍然没有响应,然后我必须手动终止它.
欢迎任何建议.