[英]Why this Python pandas DataFrame code does not work?
我的代碼:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
income_vs_hardship = %sql SELECT per_capita_income_, hardship_index FROM chicago_socioeconomic_data;
plot = sns.jointplot(x='per_capita_income_',y='hardship_index', data=pd.DataFrame(income_vs_hardship))
正確答案:
import pandas as pd
import matplotlib.pyplot as plt
%matplotlib inline
import seaborn as sns
income_vs_hardship = %sql SELECT per_capita_income_, hardship_index FROM chicago_socioeconomic_data;
plot = sns.jointplot(x='per_capita_income_',y='hardship_index', data=income_vs_hardship.DataFrame())
唯一的區別:
data=pd.DataFrame(income_vs_hardship)
vs. data=income_vs_hardship.DataFrame()
如果 DataFrame 是屬於 Pandas 的方法,為什么我的代碼不起作用。
錯誤顯示“無法解釋 per_capita_income”。
DataFrame
是 pandas 模塊的一個類,而不是可以應用於 DataFrame 實例的方法。
income_vs_hardship.DataFrame()
不能被Python 解釋,因為income_vs_hardship
沒有DataFrame
方法。 相反, pd.DataFrame(income_vs_hardship)
創建一個 DtaFrame 對象。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.