NameError:名称“x1”未在加州住房数据中定义

问题描述

这是我的代码

import pandas as pd

url = 'https://raw.githubusercontent.com/subhadipml/California-Housing-Price-Prediction/master/housing.csv'
df = pd.read_csv(url)
df

#Subset all rows where ocean_proximity is equal to NEAR BAY. Call this variable x1.
df['ocean_proximity'] = df['ocean_proximity'].str.replace('NEAR BAY','x1') 

#when I try to print x1,it shows error
print(x1.shape)

输出

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-39-6e7c2a59f460> in <module>()
      1 df['ocean_proximity'] = df['ocean_proximity'].str.replace('NEAR BAY','x1') #Subset all rows where ocean_proximity is equal to NEAR BAY. Call this variable x1.
----> 2 print(x1.shape)

NameError: name 'x1' is not defined

谁能告诉我有什么问题?

解决方法

您试图将 x1 作为变量调用,但在您的代码中,x1 是 DataFrame 中的一个字符串。你可以试试这个代码

df['ocean_proximity'] = df['ocean_proximity'].str.replace('NEAR BAY','x1')
x1 = df[['ocean_proximity']] # to get a DataFrame
print(x1.shape)
,
import pandas as pd

url = 'https://raw.githubusercontent.com/subhadipml/California-Housing-Price-Prediction/master/housing.csv'
df = pd.read_csv(url)
df

#Subset all rows where ocean_proximity is equal to NEAR BAY. Call this variable x1.
x1 = df[df['ocean_proximity'] == "NEAR BAY"]
x1.shape
->> (2290,10)
,
x1 = df[df['ocean_proximity'] == "NEAR BAY"]

x2 = df[df['ocean_proximity'] == "<1H OCEAN"]

#Make a histogram of x1 with nice titles,axes labels and colored blue.
#Make a histogram of x1 with nice titles,axes labels and colored blue.

import seaborn as sns

kwargs = dict(hist_kws={'alpha':.6},kde_kws={'linewidth':2})

plt.figure(figsize=(10,7),dpi=80)

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-39-ba84939e49fe> in <module>()
      1 #plot
      2 kwargs = dict(hist_kws={'alpha':.6},kde_kws={'linewidth':2})
----> 3 plt.figure(figsize=(10,dpi=80)

NameError: name 'plt' is not defined

plt 有什么问题?