網站首頁 編程語言 正文
Pandas count()與values_count()用法
count()
values_count()在指定的統計的列名上
結果多了該列:
對比:
對比:
Pandas:count()與value_counts()對比
1. Series.value_counts(self, normalize=False, sort=True, ascending=False, bins=None, dropna=True)
返回一個包含所有值及其數量的 Series。 且為降序輸出,即數量最多的第一行輸出。
參數含義如下:
Parameters: |
normalize : boolean, default False If True then the object returned will contain the relative frequencies of the unique values. sort : boolean, default True Sort by frequencies. ascending : boolean, default False Sort in ascending order. bins : integer, optional Rather than count values, group them into half-open bins, a convenience for pd.cut, only works with numeric data. dropna : boolean, default True Don’t include counts of NaN. |
---|---|
Returns: | Series |
舉例如下:
import pandas as pd
index = pd.Index([3, 1, 2, 3, 4, np.nan])
index.value_counts()
?
"""
輸出為:
3.0 ? ?2
4.0 ? ?1
2.0 ? ?1
1.0 ? ?1
dtype: int64
"""
如果 normalize 為 True的話,統計的結果會相加 = 1:
import pandas as pd
s = pd.Series([3, 1, 2, 3, 4, np.nan])
s.value_counts(normalize=True)
?
"""
輸出為:
3.0 ? ?0.4
4.0 ? ?0.2
2.0 ? ?0.2
1.0 ? ?0.2
dtype: float64
"""
2. ?Series.count(self, level=None)
返回非空值的數量。若是在 CSV 文件中可用來統計行數,如:
import pandas as pd
file = pd.read_csv('test.csv')
print(file['A'].count())
# 此時輸出的即是 A 列的行數
參數含義如下:?
Parameters: |
level : int or level name, default None If the axis is a MultiIndex (hierarchical), count along a particular level, collapsing into a smaller Series. |
---|---|
Returns: |
int or Series (if level specified) Number of non-null values in the Series. |
舉例如下:
import pands as pd
s = pd.Series([0.0, 1.0, np.nan])
s.count()
# 此時輸出為 2
這就是兩者的區別和各自的用途。
原文鏈接:https://blog.csdn.net/Elvirangel/article/details/104556394
相關推薦
- 2022-11-22 XML實體注入深入理解_XML示例
- 2023-12-14 【cchardet模塊】報出 “from cchardet import _cchardet Imp
- 2022-12-03 淺析nginx?客戶端返回499的錯誤碼的問題_nginx
- 2022-06-23 SQL?Server忘記sa賬號密碼重新添加新管理賬號_MsSql
- 2022-10-02 react函數組件useState異步,數據不能及時獲取到的問題_React
- 2022-11-06 pytorch簡單實現神經網絡功能_python
- 2022-10-05 Iptables防火墻string模塊擴展匹配規則_安全相關
- 2022-08-10 C語言折半查找法的超詳細講解_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支