stu = {'name':['小明','王芳','赵平','李红','李涵'], 'sex':['male','female','female','female','male'], 'year':[1996,1997,1994,1999,1996]} data = pd.DataFrame(stu) print(data['sex'].value_counts()) data['sex'].value_counts().plot(kind = 'bar',rot = 30) plt.show() data['sex'].value_counts().plot(kind = 'barh',rot = 30) plt.show() df = pd.DataFrame(np.random.randint(1,100,size = (3,3)),index = {'one','two','three'}, columns = ['I1','I2','I3']) df.plot(kind = 'barh') plt.xlabel('Values(m)') plt.ylabel('Class') plt.title('Plot Example') plt.show() wy = pd.Series(np.random.normal(size = 80)) s.hist(bins = 15,grid = False) plt.show()
时间: 2023-08-20 18:43:10 浏览: 45
这段代码是关于数据分析和可视化的。首先定义了一个字典,包含了学生的姓名、性别和出生年份等信息,然后将其转化为 Pandas 的 DataFrame。接着使用了 value_counts() 方法统计了 DataFrame 中每个性别的人数,并使用 plot() 方法绘制了柱状图和水平柱状图。然后又生成了一个随机的 DataFrame,并使用 plot() 方法绘制了水平柱状图,并加上了一些标签和标题。最后使用了 numpy 生成了一组随机数据,使用 hist() 方法绘制了直方图。
相关问题
class student(): def __init__(self,score): self.score=score def grade(self): if self.score>=90: print(4.0) elif self.score>=86: print(3.7) elif self.score>=83: print(3.3) elif self.score>=80: print(3.0) elif self.score>=76: print(2.7) elif self.score>=73: print(2.3) elif self.score>=70: print(2.0) elif self.score>=66: print(1.7) elif self.score>=63: print(1.3) elif self.score>=60: print(1.0) else: print(0) sc=int(input()) print(stu.grade())为什么这里会打印出None来!!!
你的代码中存在一个问题,即在 `print(stu.grade())` 中,你使用了 `stu` 而不是 `student`。另外,你也没有先创建一个学生对象,所以你需要先创建一个学生对象并将其分数传递给对象。修改后的代码如下:
```
class Student():
def __init__(self, score):
self.score = score
def grade(self):
if self.score >= 90:
return 4.0
elif self.score >= 86:
return 3.7
elif self.score >= 83:
return 3.3
elif self.score >= 80:
return 3.0
elif self.score >= 76:
return 2.7
elif self.score >= 73:
return 2.3
elif self.score >= 70:
return 2.0
elif self.score >= 66:
return 1.7
elif self.score >= 63:
return 1.3
elif self.score >= 60:
return 1.0
else:
return 0
score = int(input("请输入学生成绩:"))
stu = Student(score)
print(stu.grade())
```
这样就能够正确输出学生的成绩了。注意,我修改了 `grade` 方法中的 `print` 语句为 `return` 语句,并在 `print(stu.grade())` 中使用了 `return` 返回的成绩。
python 字典中 male和female
以下是一个演示如何在Python字典中筛选出特定值的例子:
```python
stu_info = {
'lisa':{'id':'001','sex':'female','age':18},
'tom':{'id':'002','sex':'male','age':19},
'vegetable':{'id':'003','sex':'male','age':18}
}
# 筛选出性别为male的学生信息
male_stu_info = {name: info for name, info in stu_info.items() if info['sex'] == 'male'}
print(male_stu_info)
# 筛选出性别为female的学生信息
female_stu_info = {name: info for name, info in stu_info.items() if info['sex'] == 'female'}
print(female_stu_info)
```
输出结果为:
```
{'tom': {'id': '002', 'sex': 'male', 'age': 19}, 'vegetable': {'id': '003', 'sex': 'male', 'age': 18}}
{'lisa': {'id': '001', 'sex': 'female', 'age': 18}}
```