首页 > 解决方案 > 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

谁能告诉我有什么问题?

标签: pythonpandas

解决方案


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,7),dpi=80)

NameError: name 'plt' is not defined

plt有什么问题?


推荐阅读