DataFrame.nunique(),DataFrame.count()

人工智能63

1. nunique()

DataFrame. nunique axis = 0dropna = True

功能:计算请求轴上的不同观察结果

参数:

  • axis: {0或'index',1或'columns'},默认为0。0或'index'用于行方式,1或'列'用于列方式。
  • dropna: bool,默认为True,不要在计数中包含NaN。

返回:Series

```python;gutter:true;

df = pd.DataFrame({'A': [1, 2, 3], 'B': [1, 1, 1]})
df.nunique()
A 3
B 1
dtype: int64


![DataFrame.nunique(),DataFrame.count()](https://johngo-pic.oss-cn-beijing.aliyuncs.com/articles/20221130/1252882-20190326133044357-1599583085.png)

```python;gutter:true;
>>> df.nunique(axis=1)
0    1
1    2
2    2
dtype: int64

2. count()

DataFrame. countaxis = 0level = Nonenumeric_only = False

功能:计算每列或每行的非NA单元格。

None,NaN,NaT和numpy.inf都被视作NA

参数:

  • axis : {0或'index',1或'columns'},默认为0(行),如果为每列生成0或'索引'计数。如果为每 生成1或'列'计数。
  • level : int或str,可选,如果轴是MultiIndex(分层),则沿特定级别计数,折叠到DataFrame中。一个STR指定级别名称。
  • numeric_only : boolean,默认为False,仅包含floatintboolean数据。

返回:Series或DataFrame对于每个列/行,非NA / null条目的数量。如果指定了level,则返回DataFrame

从字典构造DataFrame

```python;gutter:true;

df = pd.DataFrame({"Person":
... ["John", "Myla", "Lewis", "John", "Myla"],
... "Age": [24., np.nan, 21., 33, 26],
... "Single": [False, True, True, True, False]})
df
Person Age Single
0 John 24.0 False
1 Myla NaN True
2 Lewis 21.0 True
3 John 33.0 True
4 Myla 26.0 False


注意不计数的NA值

```python;gutter:true;
>>> df.count()
Person    5
Age       4
Single    5
dtype: int64

计数:

```python;gutter:true;

df.count(axis='columns')
0 3
1 2
2 3
3 3
4 3
dtype: int64


计算<cite>MultiIndex的</cite>一个级别:

```python;gutter:true;
>>> df.set_index(["Person", "Single"]).count(level="Person")
        Age
Person
John      2
Lewis     1
Myla      1

参考文献:

【1】pandas.DataFrame.count

Original: https://www.cnblogs.com/nxf-rabbit75/p/10599634.html
Author: nxf_rabbit75
Title: DataFrame.nunique(),DataFrame.count()