value_counts()
时间: 2024-05-10 07:16:58 浏览: 11
value_counts() 是 Pandas 库中一个常用的函数,它可以对 Pandas Series 或者 DataFrame 中的每个元素进行计数,并返回一个新的 Series 或者 DataFrame,其中每个元素表示原始数据中出现的次数。例如,对于一个 Pandas Series,可以使用 value_counts() 函数来统计每个元素出现的次数。下面是一个例子:
``` python
import pandas as pd
data = pd.Series([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
counts = data.value_counts()
print(counts)
```
输出结果为:
```
4 4
3 3
2 2
1 1
dtype: int64
```
这表示元素 4 出现了 4 次,元素 3 出现了 3 次,元素 2 出现了 2 次,元素 1 出现了 1 次。
相关问题
value_counts
`value_counts()` 是 Pandas 库中的一个函数,用于统计一个 Series 中每个值出现的次数并返回一个新的 Series,其中每个值是新 Series 的索引,对应的值是该值在原 Series 中出现的次数。例如:
```python
import pandas as pd
data = pd.Series([1, 2, 2, 3, 3, 3])
counts = data.value_counts()
print(counts)
```
输出结果为:
```
3 3
2 2
1 1
dtype: int64
```
表示 3 出现了 3 次,2 出现了 2 次,1 出现了 1 次。可以看出,`value_counts()` 默认按照出现次数从大到小排序。
python value_counts
Python 的 `value_counts()` 方法是 pandas 库中的一个函数,用于统计一个 Series 或 DataFrame 中每个值的出现次数。使用方法是在 Series 或 DataFrame 对象后面加上 `.value_counts()`,例如:
```
import pandas as pd
data = [1, 2, 2, 3, 3, 3]
s = pd.Series(data)
print(s.value_counts())
```
输出结果如下:
```
3 3
2 2
1 1
dtype: int64
```
也可以在Dataframe上使用value_counts(),例如:
```
df = pd.DataFrame({'A':[1,2,3,4,5], 'B':[2,2,2,2,2]})
df["B"].value_counts()
```
输出结果如下:
```
2 5
Name: B, dtype: int64
```
其中,输出结果是一个新的 Series 对象,索引是出现过的值,值是出现次数。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)