我正在嘗試根據其他資料框列中的值的比較生成一個新的資料框列。但是,當我運行以下代碼時,我收到一條錯誤訊息“只能比較標記相同的 DataFrame 物件”。就填充“建議”列而言,我在這里做錯了什么?
for Home in Target_List:
if Target_Frame.loc[[home], ['Price']] > Target_Frame.loc[[home], ['High']]:
Target_Frame.loc[[home], ['Suggest']] = "Sell"
elif Target_Frame.loc[[home], ['Price']] < Target_Frame.loc[[home], ['Low']]:
Target_Frame.loc[[home], ['Suggest']] = "Buy"
elif Target_Frame.loc[[home], ['Median']] < Target_Frame.loc[[home], ['Price']] < Target_Frame.loc[[home], ['High']]:
Target_Frame.loc[[home], ['Suggest']] = "Hold"
elif Target_Frame.loc[[home], ['Low']] < Target_Frame.loc[[home], ['Price']] < Target_Frame.loc[[home], ['Median']]:
Target_Frame.loc[[home], ['Suggest']] = "Review"
uj5u.com熱心網友回復:
如果 target_list 是一個資料框,你可以像這樣做你想做的事:
target_frame = pd.DataFrame()
for i, row in target_list.iterrows():
if row["Price"] > row["High"]:
target_frame.at[i, "Suggest"] = "Sell"
elif row["Price"] < row["Low"]:
target_frame.at[i, "Suggest"] = "Buy"
elif row["Median"] < row["Price"] < row["High"]:
target_frame.at[i, "Suggest"] = "Hold"
elif row["Low"] < row["Price"] < row["Median"]:
target_frame.at[i, "Suggest"] = "Review"
uj5u.com熱心網友回復:
您可以使用apply函式顯著簡化邏輯:
使用:
home High Low Median Price
A 200 100 130 220
B 220 120 150 100
C 300 210 240 230
D 400 280 350 380
E 300 210 240 200
F 200 100 130 220
G 220 120 150 240
H 300 210 240 270
I 400 280 350 380
使用簡化的邏輯創建您的建議函式:
def suggester(x):
if x['Price'] > x['High']:
return "Sell"
elif x['Price'] < x['Low']:
return "Buy"
elif x['Median'] < x['Price']:
return "Hold"
return "Review"
目標房屋:
target_list = list('ABCDGH')
應用該功能并獲得您的建議:
df['Suggest'] = df[df['home'].isin(target_list)].apply(suggester, axis=1)
print(df)
home High Low Median Price Suggest
0 A 200 100 130 220 Sell
1 B 220 120 150 100 Buy
2 C 300 210 240 230 Review
3 D 400 280 350 380 Hold
4 E 300 210 240 200 NaN
5 F 200 100 130 220 NaN
6 G 220 120 150 240 Sell
7 H 300 210 240 270 Hold
8 I 400 280 350 380 NaN
uj5u.com熱心網友回復:
這是一種避免回圈并在某些方面通過覆寫簡化建議邏輯的方法
import pandas as pd
import numpy as np
#Create example data
np.random.seed(1)
num_houses = 20
house_names = ['house_{}'.format(i 1) for i in range(num_houses)]
house_prices = np.random.randint(100,1000,num_houses)
low_prices = np.random.randint(100,1000,num_houses)
high_prices = low_prices np.random.randint(100,1000,num_houses)
med_prices = (low_prices high_prices)/2
Target_Frame = pd.DataFrame({
'Home':house_names,
'Price':house_prices,
'High':high_prices,
'Low':low_prices,
'Median':med_prices,
})
#First suggest to buy everything
Target_Frame['Suggest'] = 'Buy'
#If the price is more than the low, overwrite these rows with 'Review'
Target_Frame.loc[Target_Frame['Price'] > Target_Frame['Low'], 'Suggest'] = 'Review'
#If the price is more than the median, overwrite these rows with 'Hold'
Target_Frame.loc[Target_Frame['Price'] > Target_Frame['Median'], 'Suggest'] = 'Hold'
#If the price is more than the High, overwrite these rows with 'Sell'
Target_Frame.loc[Target_Frame['Price'] > Target_Frame['High'], 'Suggest'] = 'Sell'
Target_Frame
輸出:

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/477840.html
下一篇:python通過鏈接快速回圈
