df = pd.read_csv('stock_data.csv') if len(df) >= 4 and window_size > 0: df['four_days_increase'] = df['close'].rolling(window=4).apply(lambda x: all(x[i] < x[i+1] for i in range(3))) * 1 if len(df) >= 3 and window_size > 0: df['three_days_decrease'] = df['close'].rolling(window=3).apply(lambda x: all(x[i] > x[i+1] for i in range(2))) * 1更正使上述代码在jupyter中不报错
时间: 2024-02-29 12:52:20 浏览: 48
解决pandas使用read_csv()读取文件遇到的问题
在 Jupyter 中运行代码时,需要确保代码块的缩进正确,同时需要确保代码块的语法正确。可以将代码块放在一个代码单元中运行,如下所示:
```
import pandas as pd
window_size = 4 # 定义窗口大小
df = pd.read_csv('stock_data.csv')
if len(df) >= window_size and window_size > 0:
df['four_days_increase'] = df['close'].rolling(window=window_size).apply(lambda x: all(x[i] < x[i+1] for i in range(window_size-1))) * 1
if len(df) >= window_size-1 and window_size > 0:
df['three_days_decrease'] = df['close'].rolling(window=window_size-1).apply(lambda x: all(x[i] > x[i+1] for i in range(window_size-2))) * 1
```
注意,代码中需要先导入 pandas 库,否则会报错。同时需要确保代码块的缩进正确,以及 window_size 变量的值正确。
阅读全文