language=Series(["Python","C","Scala","Java","Go","Scala","SQL","PHP","Python"])统计每种语言出现的次数
时间: 2023-07-02 12:13:28 浏览: 93
可以使用Python中的collections模块中的Counter类来实现:
```python
from collections import Counter
language = ["Python", "C", "Scala", "Java", "Go", "Scala", "SQL", "PHP", "Python"]
language_counter = Counter(language)
print(language_counter)
```
输出结果为:
```
Counter({'Python': 2, 'Scala': 2, 'C': 1, 'Java': 1, 'Go': 1, 'SQL': 1, 'PHP': 1})
```
其中,Counter类会返回一个字典,其中key为元素,value为元素出现的次数。可以看到,Python和Scala都出现了2次,其余语言都出现了1次。
相关问题
import numpy as np import pandas as pd array=["Python","C","Scala","Java","GO","Scala","SQL","PHP","Python"] language=pd.Series(data=array) print(language) score=pd.Series(np.random.randint(low=0,high=100,size=9)) print(score) df_series1=language print(pd.DataFrame(df_series1)) df_series2=score print(pd.DataFrame(df_series2)) integrate=pd.concat([pd.DataFrame(df_series1),pd.DataFrame(df_series2)],axis=1,ignore_index=True) print(integrate) print(integrate.head(4))完成下面的题目:(5) 输出language字段为Python的行(6) 按照score字段升序排序(7) 统计每种编程语言出现的次数
(5) 输出language字段为Python的行:
```python
print(integrate[integrate[0]=="Python"])
```
输出:
```
0 1
0 Python 24
8 Python 60
```
(6) 按照score字段升序排序:
```python
print(integrate.sort_values(by=1))
```
输出:
```
0 1
0 Python 24
2 Scala 25
6 SQL 26
1 C 28
3 Java 30
7 PHP 37
5 Scala 41
4 GO 56
8 Python 60
```
(7) 统计每种编程语言出现的次数:
```python
print(integrate[0].value_counts())
```
输出:
```
Scala 2
Python 2
C 1
GO 1
Java 1
SQL 1
PHP 1
Name: 0, dtype: int64
```
python中pandas数据清洗 根据列["Python'”,"c","Scala”',"Java","Go"•Scala",'SQL", PFIP","Python]创建一个变量名为language的Series;创建一个由随机整型组成的Series, 要求长度与language相同,变量名为score;根据language和score创建一个DataFrame;输出该DataFrame的前4行数据;输出该DataFrame中language字段为Python的行;将DataFrame按照score字段的值进行升序排序;统计language字段中每种编程语言出现的次数。
可以按照以下步骤来实现:
1. 创建Series:language = pd.Series(["Python","c","Scala","Java","Go","Scala","SQL","PFIP","Python"])
2. 创建Series:score = pd.Series(np.random.randint(0, 100, len(language)))
3. 创建DataFrame:df = pd.DataFrame({'language': language, 'score': score})
4. 输出前4行数据:print(df.head(4))
5. 输出language字段为Python的行:print(df[df['language'] == 'Python'])
6. 按照score字段升序排序:df = df.sort_values('score', ascending=True)
7. 统计language字段中每种编程语言出现的次数:print(df['language'].value_counts())
完整代码如下:
```python
import pandas as pd
import numpy as np
# 创建Series
language = pd.Series(["Python","c","Scala","Java","Go","Scala","SQL","PFIP","Python"])
# 创建Series
score = pd.Series(np.random.randint(0, 100, len(language)))
# 创建DataFrame
df = pd.DataFrame({'language': language, 'score': score})
# 输出前4行数据
print(df.head(4))
# 输出language字段为Python的行
print(df[df['language'] == 'Python'])
# 按照score字段升序排序
df = df.sort_values('score', ascending=True)
# 统计language字段中每种编程语言出现的次数
print(df['language'].value_counts())
```
阅读全文