我想將一列的每個唯一值一一傳遞給用戶定義的函式并獲取結果。唯一值是字串,函式基于 query()。下面是我是如何做到的,但它不起作用。即我不能將“u”傳遞給查詢。這段代碼有什么問題?提前謝謝
def score_effect(platform):
df = data.query('platform=="p"')[['sum_of_sales', 'critic_score', 'user_score']]
plt.figure(figsize=(17,10))
plt.subplot(1,2,1)
sns.scatterplot(data=df, x='sum_of_sales', y='critic_score')
plt.subplot(1,2,2)
sns.scatterplot(data=df, x='sum_of_sales', y='user_score')
plt.show()
critic_corr = df.corr().iloc[0,1]
user_corr = df.corr().iloc[0,2]
print('Correlation between critic-reviews and sales =', critic_corr)
print('Correlation between user-reviews and sales =', user_corr)
if critic_corr>0.7:
print('There is a POSITIVE correlation between critic-reviews and sales')
else:
print('There is no significant correlation between critic-reviews and sales')
if user_corr>0.7:
print('There is a POSITIVE correlation between user-reviews and sales')
else:
print('There is no significant correlation between user-reviews and sales')
for p in data['platform'].unique():
print('Result for:', p)
score_effect(p)
print('***************************')
uj5u.com熱心網友回復:
看起來你應該更換
data.query('platform=="p"')
和
data.query('platform == @platform')
對于初學者。您的函式的引數是平臺,而不是p. p是for回圈中的變數名稱,但函式作用域不同,因此您的函式無法訪問p.
您還應該@像我上面所做的那樣在變數前加上一個符號,以pandas表明它是一個變數,而不是字串文字“平臺”。從檔案:
您可以通過在變數前加上“@”字符來參考環境中的變數,例如
@a b。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/368961.html
上一篇:功能誤解
