for j in range(1,8): df_data = [] for i in range(0,14): today = date.today() day = today - timedelta(days= i) data_1 = data_price[(pd.to_datetime(data_price['日期']) >= str(day)) & (pd.to_datetime(data_price['日期']) <= str(day))] price_data = pd.DataFrame() price_data['date'] = pd.to_datetime([day]) price_data['real'] = round(data_1['日前价格'].mean(),2) file_path = os.path.join(f'{folders3}/D{j}', f"{day}.xlsx") if not os.path.exists(file_path): continue price_data['pred'] = round(np.mean(pd.read_excel(file_path)),2) file_path2 = os.path.join(f'{folders3}/model2/D{j}', f"{day}.xlsx") if not os.path.exists(file_path2): continue price_data['pred2'] = round(np.mean(pd.read_excel(file_path2)['yhat']),2) price_data['advice'] = price_data['pred'].values*0.6 +price_data['pred2'].values*0.4 df_data.append(price_data) matrix1 = pd.concat(df_data,ignore_index=True)
时间: 2024-02-06 13:04:41 浏览: 77
根据你提供的代码,可能出现"No objects to concatenate"的情况是在以下代码行:
```
matrix1 = pd.concat(df_data,ignore_index=True)
```
可能是因为在循环中,有些条件下 `df_data` 列表为空,所以在最后一次循环结束后,`df_data` 仍然为空。这就导致了在对空列表进行连接操作时出现了"No objects to concatenate"的提示。
为了避免这种情况,你可以在每一次循环前先清空 `df_data` 列表,如下所示:
```
for j in range(1,8):
df_data = []
for i in range(0,14):
# ...
df_data.append(price_data)
if len(df_data) == 0:
continue
matrix1 = pd.concat(df_data, ignore_index=True)
# ...
```
这样就可以避免在连接空列表时出现"No objects to concatenate"的提示了。
相关问题
#name_one = [fake.name() for i in range(100)] name_two = [fake.name() for i in range(20)] #date_list = [fake.date_between(start_date='-1y', end_date='today') for i in range(100)] data_df1=pd.read_excel(r"D:\xxx.xls") #print(data_df1.columns) data_df2=(data_df1["部门"].unique()) print(data_df2) for i in range(150): ks_list=[data_df2] if random.random() < 0.5: prefix = random.choice(ks_list) data_df3 = prefix +str(" ") + name_two print(data_df3) 修改
根据您的代码,我看到您想要生成一些假的数据并进行一些处理。如果您想要将 `name_two` 列表中的每个元素与 `data_df2` 中的每个元素随机组合,可以尝试以下代码:
```python
import random
from faker import Faker
import pandas as pd
fake = Faker()
name_one = [fake.name() for i in range(100)]
name_two = [fake.name() for i in range(20)]
data_df1 = pd.read_excel(r"D:\xxx.xls")
data_df2 = data_df1["部门"].unique()
data_df3 = []
for i in range(150):
prefix = random.choice(data_df2)
if random.random() < 0.5:
suffix = random.choice(name_two)
else:
suffix = random.choice(name_one)
data_df3.append(prefix + " " + suffix)
data_df3 = pd.DataFrame(data_df3, columns=["新员工姓名"])
print(data_df3)
```
这样,您会得到一个名为 `data_df3` 的 Pandas 数据帧,其中包含了 `data_df2` 中的每个元素和 `name_one` 或 `name_two` 中的随机元素组合而成的新员工姓名。
import pandas as pd df = pd.read_csv('stock_data.csv') df['four_days_increase'] = df['close'].rolling(window=4).apply(lambda x: all(x[i] < x[i+1] for i in range(3))) * 1 df['three_days_decrease'] = df['close'].rolling(window=3).apply(lambda x: all(x[i] > x[i+1] for i in range(2))) * 1 capital = 1000000 max_stock_per_day = 10 max_stock_value = 100000 start_date = '2020-01-01' end_date = '2023-01-01' df = df[(df['date'] >= start_date) & (df['date'] < end_date)] df = df.reset_index(drop=True) hold_stock = [] for i, row in df.iterrows(): if len(hold_stock) > 0: sell_stock = [] for stock in hold_stock: if i - stock['buy_day'] >= 3: capital += stock['buy_price'] * stock['buy_qty'] * (1 - 0.002) sell_stock.append(stock) hold_stock = [stock for stock in hold_stock if stock not in sell_stock] df_today = df.loc[i:i+3] if i + 3 >= len(df): break if all(df_today['four_days_increase']) and all(df_today['three_days_decrease'].iloc[1:]): available_capital = capital available_stock = max_stock_per_day available_value = max_stock_value for j, stock_row in df_today.iterrows(): if available_capital > 0 and available_stock > 0 and available_value > 0: buy_qty = min(int(available_capital / (stock_row['close'] * 1.002)), available_stock, int(available_value / (stock_row['close'] * 1.002))) if buy_qty > 0: hold_stock.append({'buy_day': i, 'buy_price': stock_row['close'], 'buy_qty': buy_qty}) available_capital -= stock_row['close'] * buy_qty * 1.002 available_stock -= 1 available_value -= stock_row['close'] * buy_qty * 1.002 print('Final capital:', capital)让上述代码在jupyter里不报错
这段代码的错误可能是由于缺少依赖库或文件。请确保您已经安装了pandas库并且已经将stock_data.csv文件放在正确的位置。另外,您可能需要在Jupyter Notebook中使用`%matplotlib inline`来确保图表正确显示。以下是可能的修改:
```
import pandas as pd
%matplotlib inline
df = pd.read_csv('stock_data.csv')
df['four_days_increase'] = df['close'].rolling(window=4).apply(lambda x: all(x[i] < x[i+1] for i in range(3))) * 1
df['three_days_decrease'] = df['close'].rolling(window=3).apply(lambda x: all(x[i] > x[i+1] for i in range(2))) * 1
capital = 1000000
max_stock_per_day = 10
max_stock_value = 100000
start_date = '2020-01-01'
end_date = '2023-01-01'
df = df[(df['date'] >= start_date) & (df['date'] < end_date)]
df = df.reset_index(drop=True)
hold_stock = []
for i, row in df.iterrows():
if len(hold_stock) > 0:
sell_stock = []
for stock in hold_stock:
if i - stock['buy_day'] >= 3:
capital += stock['buy_price'] * stock['buy_qty'] * (1 - 0.002)
sell_stock.append(stock)
hold_stock = [stock for stock in hold_stock if stock not in sell_stock]
df_today = df.loc[i:i+3]
if i + 3 >= len(df):
break
if all(df_today['four_days_increase']) and all(df_today['three_days_decrease'].iloc[1:]):
available_capital = capital
available_stock = max_stock_per_day
available_value = max_stock_value
for j, stock_row in df_today.iterrows():
if available_capital > 0 and available_stock > 0 and available_value > 0:
buy_qty = min(int(available_capital / (stock_row['close'] * 1.002)), available_stock, int(available_value / (stock_row['close'] * 1.002)))
if buy_qty > 0:
hold_stock.append({'buy_day': i, 'buy_price': stock_row['close'], 'buy_qty': buy_qty})
available_capital -= stock_row['close'] * buy_qty * 1.002
available_stock -= 1
available_value -= stock_row['close'] * buy_qty * 1.002
print('Final capital:', capital)
```
阅读全文