更新到 Python 3.10 后,我現在在使用 frame.append 將字典附加到 Pandas DataFrame 時收到以下警告。
FutureWarning: The frame.append method is deprecated and will be
removed from pandas in a future version. Use pandas.concat instead.
下面是代碼。這仍然有效,但我想解決警告。
report = report.append({
"period":period,
"symbol":symbol,
"start_date":start_date,
"start_price":start_price,
"start_market_cap":start_market_cap,
"end_date":end_date,
"end_price":end_price,
"end_market_cap":end_market_cap,
"return":return_
},ignore_index=True)
我已將代碼更新為以下內容,這會引發不同的警告:
report = pd.concat([report,{
"period":period,
"symbol":symbol,
"start_date":start_date,
"start_price":start_price,
"start_market_cap":start_market_cap,
"end_date":end_date,
"end_price":end_price,
"end_market_cap":end_market_cap,
"return":return_
}],ignore_index=True)
TypeError: cannot concatenate object of type '<class 'dict'>'; only Series and DataFrame objs are valid
2個問題:第一個警告錯了嗎?3.10 的實作方式是什么?謝謝。
uj5u.com熱心網友回復:
用于loc分配單行值:
report.loc[len(report)] = {"period":period,
"symbol":symbol,
"start_date":start_date,
"start_price":start_price,
"start_market_cap":start_market_cap,
"end_date":end_date,
"end_price":end_price,
"end_market_cap":end_market_cap,
"return":return_
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/446235.html
標籤:Python 熊猫 级联 python-3.10
