简体   繁体   English

为什么这个 Python pandas DataFrame 代码不起作用?

[英]Why this Python pandas DataFrame code does not work?

My code:我的代码:

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))

Correct answer:正确答案:

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())

The only difference:唯一的区别:
data=pd.DataFrame(income_vs_hardship) vs. data=income_vs_hardship.DataFrame() data=pd.DataFrame(income_vs_hardship) vs. data=income_vs_hardship.DataFrame()

If DataFrame is a method belongs to pandas, why my code does not work.如果 DataFrame 是属于 Pandas 的方法,为什么我的代码不起作用。
The error shows 'unable to interpret the per_capita_income.'错误显示“无法解释 per_capita_income”。

DataFrame is a class of the pandas module, not a method that you can apply to a DataFrame instance. DataFrame是 pandas 模块的一个类,而不是可以应用于 DataFrame 实例的方法。

income_vs_hardship.DataFrame() can't be interpreted by Python, as income_vs_hardship has no DataFrame method. income_vs_hardship.DataFrame()不能被Python 解释,因为income_vs_hardship没有DataFrame方法。 Instead, pd.DataFrame(income_vs_hardship) creates a DtaFrame object.相反, pd.DataFrame(income_vs_hardship)创建一个 DtaFrame 对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM