简体   繁体   中英

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

If DataFrame is a method belongs to pandas, why my code does not work.
The error shows 'unable to interpret the per_capita_income.'

DataFrame is a class of the pandas module, not a method that you can apply to a DataFrame instance.

income_vs_hardship.DataFrame() can't be interpreted by Python, as income_vs_hardship has no DataFrame method. Instead, pd.DataFrame(income_vs_hardship) creates a DtaFrame object.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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