(1)根据列表["Python","C","Scala","Java","GO","Scala","SQL","PHP","Python"]创建一个变量名为language的Series; (2)创建一个由随机整型组成的Series,要求长度与language相同,变量名为score; (3)根据language和score创建一个DataFrame; (4)输出该DataFrame的前4行数据; (5)输出该DataFrame中language字段为Python的行; (6)将DataFrame按照score字段的值进行升序排序; (7)统计language字段中每种编程语言出现的次数。
时间: 2023-07-02 20:13:34 浏览: 228
python+spark+大数据
好的,以下是您的回答:
```python
import pandas as pd
import numpy as np
# (1) 根据列表["Python","C","Scala","Java","GO","Scala","SQL","PHP","Python"]创建一个变量名为language的Series
language = pd.Series(["Python","C","Scala","Java","GO","Scala","SQL","PHP","Python"])
# (2) 创建一个由随机整型组成的Series,要求长度与language相同,变量名为score
score = pd.Series(np.random.randint(0, 100, len(language)))
# (3) 根据language和score创建一个DataFrame
df = pd.DataFrame({"language": language, "score": score})
# (4) 输出该DataFrame的前4行数据
print(df.head(4))
# (5) 输出该DataFrame中language字段为Python的行
print(df[df["language"] == "Python"])
# (6) 将DataFrame按照score字段的值进行升序排序
df = df.sort_values("score", ascending=True)
# (7) 统计language字段中每种编程语言出现的次数
count = df["language"].value_counts()
print(count)
```
希望能够帮到您!
阅读全文