Python错误“NameError:未定义名称“a”

2024-04-24 00:15:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试编写一个脚本,使用python上的pandas提取名为PopChange.CSV和Housing.CSV的两个CSV文件。我正在选择要从中选择的选项,但不断收到此错误消息。不知道我做错了什么

我正在尝试执行的项目提供了选项,并将CSV文件打印出来或将结果显示在直方图中:

***************** Welcome to the Python Data Analysis App**********

Select the file you want to analyze:
1. Population Data
2. Housing Data
3. Exit the Program

1

You have entered Population Data.

Select the Column you want to analyze:

a. Pop Apr 1

b. Pop Jul 1

c. Change Pop

d. Exit Column

a

You selected Pop Apr 1

The statistics for this column are:

Count = 10000
Mean = 32.5
Standard Deviation = 4.5
Min = 53.2
Max = 12.5
The Histogram of this column is now displayed.

Select the Column you want to analyze:
a. Pop Apr 1
b. Pop Jul 1
c. Change Pop
d. Exit Column 

d
You selected to exit the column menu

Select the file you want to analyze:

1. Population Data

2. Housing Data

3. Exit the Program

3

*************** Thanks for using the Data Analysis App**********

下面是我的代码,我有问题,但不完全理解为什么。我将数字与x=int部分一起使用,这是(如果x=1,如果x=2),但是我尝试使用字母作为字母选项,并得到错误

我的代码:

`
import pandas as pd             
import matplotlib.pyplot as plt 

print("****Welcome to the python data analysis app****")

x=int(input("Please enter file you wish to review:\n 1. Population Data\n 2. Housing Data\n 3. Exit 
the program\n "))


if x==1:
  print("You have entered Population Data.")

  y=str(input("Select the Column you want to analyze:\n a. Pop Apr 1\n b. Pop Jul 1\n c. Change Pop\n 
  d. Exit Column\n"))

  if y==a: 
    popchange=pd.read_csv("PopChange.csv") 
    popchange_selected = popchange[["Pop Apr 1"]]
    popchange_selected.hist(bins=50, figsize=(20,15)) 
    plt.show() 
    print(popchange_selected.describe())

  if y==b:
    popchange=pd.read_csv("PopChange.csv") 
    popchange_selected = popchange[["Pop Jul 1"]] 
    popchange_selected.hist(bins=50, figsize=(20,15)) 
    plt.show()
    print(popchange_selected.describe())

  if y==c:
    popchange=pd.read_csv("PopChange.csv")
    popchange_selected = popchange[["Change Pop"]]
    popchange_selected.hist(bins=50, figsize=(20,15)) 
    plt.show()
    print(popchange_selected.describe())

  if y==d:
    x=int() 


if x==2:
  housing=pd.read_csv("Housing.csv")
  housing_selected=housing[["AGE", "BEDRMS", "BUILT", "ROOMS", "UTILITY"]]
  housing_selected.hist(bins=50, figsize=(20,15))
  plt.show()
  print(housing_selected.describe())
`

错误消息:

File "main.py", line 13, in <module>

if y==a: 

NameError: name 'a' is not defined

Tags: csvthetoyoudataifexitcolumn
1条回答
网友
1楼 · 发布于 2024-04-24 00:15:28

a未在代码中的任何位置定义

也许你的意思是'a'(字符串),在这种情况下,你应该用引号把它括起来

例如

  if y=='a':  # string literal
    popchange=pd.read_csv("PopChange.csv") 
    popchange_selected = popchange[["Pop Apr 1"]]
    popchange_selected.hist(bins=50, figsize=(20,15)) 
    plt.show() 
    print(popchange_selected.describe())

  if y=='b':
    popchange=pd.read_csv("PopChange.csv") 
    popchange_selected = popchange[["Pop Jul 1"]] 
    popchange_selected.hist(bins=50, figsize=(20,15)) 
    plt.show()
    print(popchange_selected.describe())

  if y=='c':
    popchange=pd.read_csv("PopChange.csv")
    popchange_selected = popchange[["Change Pop"]]
    popchange_selected.hist(bins=50, figsize=(20,15)) 
    plt.show()
    print(popchange_selected.describe())

  if y=='d':
    x=int() 

相关问题 更多 >