import pandas as pd
columns_list=['sport','home','away','selection','odds','tipster','stake',
'is_won','profit','bookie']
df = pd.DataFrame(columns=columns_list)
def bet(sport,home,away,selection,odds,tipster,stake,profit,bookie):
if profit > 0:
is_won = True
elif profit < 0:
is_won = False
temp_df = pd.DataFrame([[sport,home,away,selection,odds,tipster,stake,
is_won,profit,bookie]], columns=columns_list)
df.append(temp_df, ignore_index=True)
#print(smemels has been inserted)
#add market
bet(sport="football",home="sporting",away="porto",selection="sporting",
odds=2.7,tipster="me",stake=500,profit=500,bookie="marathon")
我想創建一個空的DataFrame
,然後追加新行,通過創建一個函數,所以我只需要把值,它會自動插入。當我運行代碼bet(...)
它並不真正追加數據。如何創建一個函數將數據附加到數據框?
您需要從函數返回df,並調用爲df = bet(..)。它會創建一個當前丟棄的新對象。 – roganjosh
對不起,'return df.append(temp_df,ignore_index = True)' – roganjosh
這不是我要找的 –