我想创建一个"big"df使用df_temp我在每次迭代中得到
:
df_data = {
'Code':[data['code']],
'Buy_date':[date_ex_dividend],
'Buy':[round(df.loc[date_ex_dividend]['Open'],2)],
'Sell_date':[date_pay_dividend],
'Sell':[round(df.loc[date_pay_dividend]['Close'],2)],
'Div_profit':[data['profit']],
'Gap':[round(sell-bank,2)],
'TOTAL_PROFIT':[round(sell-bank + (bank*percent/100),2)],
}
df_temp = pd.DataFrame(df_data, index={counter},columns = [
'Code',
'Buy_date',
'Buy',
'Sell_date',
'Sell',
'Div_profit',
'Gap',
'TOTAL_PROFIT',
])
counter +=1
print (df_temp)
3个迭代输出示例:
Code Buy_date Buy Sell_date Sell Div_profit Gap TOTAL_PROFIT
0 CIE.MC 2018-01-03 21.66 2018-01-05 22.65 1,70% 46.13 63.13
Code Buy_date Buy Sell_date Sell Div_profit Gap TOTAL_PROFIT
1 REE.MC 2018-01-03 18.27 2018-01-05 18.92 5,44% 35.3 89.7
Code Buy_date Buy Sell_date Sell Div_profit Gap TOTAL_PROFIT
2 ZOT.MC 2018-01-08 9.33 2018-01-10 9.25 2,93% -8.57 20.73
知道如何将所有迭代连接到一个大DF中吗?非常感谢!!
>>> col_names = ['Code', 'Buy_date', 'Buy', 'Sell_date', 'Sell', 'Div_profit', 'Gap', 'TOTAL_PROFIT']
>>> df = pd.DataFrame(columns=col_names)
# within the loop:
df = pd.concat([df, df_temp])
同样,最好将所有循环变量包含在一个列表中,您应该从中构建一次数据框架(不使用concat)