Pandas DataFrame.count(~)方法計算數量非缺失值對於 DataFrame 的每一行或每一列。
參數
1.axis | string 或 int | optional
是否檢查每列或行:
| 軸 | 說明 | 
|---|---|
| 
 | 計算每一列。 | 
| 
 | 計算每一行。 | 
默認情況下,axis=0 。
2. level | int 或 string | optional
要檢查的級別。僅當源 DataFrame 具有 MultiIndex 時,這才相關。
3. numeric_only | boolean | optional
- 
如果 True,則該方法將對number或boolean類型的列/行執行計數。
- 
如果 False,則所有列/行都將被計數。
默認情況下,numeric_only=False 。
返回值
int 的 Series 指示源 DataFrame 每行/列的缺失值數量。
例子
考慮以下 DataFrame :
df = pd.DataFrame({"A":[pd.np.nan,pd.np.nan], "B":[3,4]})
df
   A    B
0  NaN  3
1  NaN  4按列計算非缺失值
要計算每列的非缺失值數量:
df.count()   # axis=0
A  0
B  2
dtype: int64在這裏,A 列中有 0 非 NaN 值,B 中有 2 個非 NaN 值。
按行計算非缺失值
要計算每行的非缺失值數量,請設置 axis=1 :
df.count(axis=1)
0    1
1    1
dtype: int64在這裏,行 0 和行 1 中都有 1 個非缺失值。
僅計算數字和布爾列/行
考慮以下 DataFrame :
df = pd.DataFrame({"A":["a","b"], "B":[3,4]})
df
   A  B
0  a  3
1  b  4要僅計算數字和布爾列,請設置 numeric_only=True :
df.count(numeric_only=True)
B    2
dtype: int64請注意 A 列如何被忽略,因為它是非數字類型。
相關用法
- Python PySpark DataFrame count方法用法及代碼示例
- Python Pandas DataFrame copy方法用法及代碼示例
- Python PySpark DataFrame collect方法用法及代碼示例
- Python PySpark DataFrame coalesce方法用法及代碼示例
- Python Pandas DataFrame corrwith方法用法及代碼示例
- Python PySpark DataFrame corr方法用法及代碼示例
- Python Pandas DataFrame convert_dtypes方法用法及代碼示例
- Python Pandas DataFrame combine方法用法及代碼示例
- Python Pandas DataFrame columns屬性用法及代碼示例
- Python PySpark DataFrame cov方法用法及代碼示例
- Python PySpark DataFrame colRegex方法用法及代碼示例
- Python PySpark DataFrame columns屬性用法及代碼示例
- Python Pandas DataFrame corr方法用法及代碼示例
- Python Pandas DataFrame combine_first方法用法及代碼示例
- Python Pandas DataFrame cov方法用法及代碼示例
- Python Pandas DataFrame clip方法用法及代碼示例
- Python Pandas DataFrame cummax方法用法及代碼示例
- Python Pandas DataFrame cumprod方法用法及代碼示例
- Python Pandas DataFrame cummin方法用法及代碼示例
- Python Pandas DataFrame cumsum方法用法及代碼示例
- Python Pandas DataFrame empty屬性用法及代碼示例
- Python Pandas DataFrame pop方法用法及代碼示例
- Python Pandas DataFrame nsmallest方法用法及代碼示例
- Python Pandas DataFrame sample方法用法及代碼示例
- Python Pandas DataFrame items方法用法及代碼示例
注:本文由純淨天空篩選整理自Isshin Inada大神的英文原創作品 Pandas DataFrame | count method。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
