Hướng dẫn count nan in list python - đếm nan trong danh sách python

2

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Làm thế nào để tôi đếm được sự xuất hiện của NaN trong danh sách của tôi? Dữ liệu của tôi trông như thế này:

[365788, 267102, 170941, 'NaN', 243293, 267093, 'NaN', 370448, 'NaN', 197091]

Hướng dẫn count nan in list python - đếm nan trong danh sách python

Mark Amery

133K78 Huy hiệu vàng395 Huy hiệu bạc443 Huy hiệu Đồng78 gold badges395 silver badges443 bronze badges

Đã hỏi ngày 24 tháng 3 năm 2017 lúc 17:17Mar 24, 2017 at 17:17

Hướng dẫn count nan in list python - đếm nan trong danh sách python

2

Bạn có thể sử dụng collections.Counter:

from collections import Counter

Counter(yourlist)

Bạn sẽ có số lần xuất hiện cho mọi giá trị duy nhất trong danh sách của bạn

Một lựa chọn khác là:

Sử dụng phương thức count của danh sách:

yourlist.count('Nan')

Đã trả lời ngày 24 tháng 3 năm 2017 lúc 17:19Mar 24, 2017 at 17:19

LoicmloicmLoicM

1.55316 huy hiệu bạc31 huy hiệu đồng16 silver badges31 bronze badges

6

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Trong bài viết này, chúng ta sẽ xem làm thế nào để đếm số lượng các yếu tố không phải là nan trong một mảng numpy trong Python.

    NAN: Nó được sử dụng khi bạn không quan tâm đến giá trị ở vị trí đó. Có thể đôi khi được sử dụng thay cho dữ liệu bị thiếu hoặc dữ liệu bị hỏng. & NBSP;It is used when you don’t care what the value is at that position. Maybe sometimes is used in place of missing data, or corrupted data. 

    Phương pháp 1: Sử dụng điều kiện

    Trong ví dụ này, chúng tôi sẽ sử dụng các mảng một chiều. Trong mã được đưa ra dưới đây, chúng tôi lặp qua mọi mục nhập của mảng numpy đã cho và kiểm tra xem giá trị có phải là NAN hay không.

    Python3

    import

    from collections import Counter
    
    Counter(yourlist)
    
    0

    from collections import Counter
    
    Counter(yourlist)
    
    1
    from collections import Counter
    
    Counter(yourlist)
    
    2
    from collections import Counter
    
    Counter(yourlist)
    
    3
    from collections import Counter
    
    Counter(yourlist)
    
    4
    from collections import Counter
    
    Counter(yourlist)
    
    5
    from collections import Counter
    
    Counter(yourlist)
    
    6
    from collections import Counter
    
    Counter(yourlist)
    
    5
    from collections import Counter
    
    Counter(yourlist)
    
    8
    from collections import Counter
    
    Counter(yourlist)
    
    9
    3
    5
    0

    3
    5
    1
    from collections import Counter
    
    Counter(yourlist)
    
    2
    from collections import Counter
    
    Counter(yourlist)
    
    3
    from collections import Counter
    
    Counter(yourlist)
    
    4
    from collections import Counter
    
    Counter(yourlist)
    
    5
    3
    5
    6
    from collections import Counter
    
    Counter(yourlist)
    
    5
    from collections import Counter
    
    Counter(yourlist)
    
    8
    3
    5
    9
    4
    0
    4
    1
    4
    2

    4
    3
    from collections import Counter
    
    Counter(yourlist)
    
    8
    4
    5
    4
    1
    3
    5
    0

    4
    8
    4
    9

    22
    0
    22
    1
    from collections import Counter
    
    Counter(yourlist)
    
    2 ________ 43 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;

    22
    0
    22
    5
    22
    6
    22
    7
    22
    8

    22
    9NaN0 NaN1 NaN2

    NaN3

    22
    1NaN55____12 ________ 14 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;

    22
    0NaN9 count

    collections.Counter1collections.Counter2

    collections.Counter1collections.Counter4

    Output:

    3
    5

    Phương pháp 2: Sử dụng isnan ()

    Sử dụng chức năng của các mảng numpy, chúng ta có thể thực hiện một thao tác trên toàn bộ mảng cùng một lúc, thay vì một phần tử duy nhất.

    Chức năng đã sử dụng:

    • np.isnan (dữ liệu): Trả về một mảng boolean sau khi thực hiện thao tác np.isnan () trên các mục của mảng, dữ liệu
    • np.sum (): Vì chúng tôi đang nhập một mảng boolean vào hàm tổng, nên nó trả về số lượng giá trị thực (1s) trong mảng bool.

    Python3

    import

    from collections import Counter
    
    Counter(yourlist)
    
    0

    from collections import Counter
    
    Counter(yourlist)
    
    1
    from collections import Counter
    
    Counter(yourlist)
    
    2
    from collections import Counter
    
    Counter(yourlist)
    
    3
    from collections import Counter
    
    Counter(yourlist)
    
    4
    from collections import Counter
    
    Counter(yourlist)
    
    5
    from collections import Counter
    
    Counter(yourlist)
    
    6
    from collections import Counter
    
    Counter(yourlist)
    
    5
    from collections import Counter
    
    Counter(yourlist)
    
    8
    from collections import Counter
    
    Counter(yourlist)
    
    9
    3
    5
    0

    4
    3count8
    from collections import Counter
    
    Counter(yourlist)
    
    8yourlist.count('Nan')0yourlist.count('Nan')1

    3
    5
    1
    from collections import Counter
    
    Counter(yourlist)
    
    2
    from collections import Counter
    
    Counter(yourlist)
    
    3
    from collections import Counter
    
    Counter(yourlist)
    
    4
    from collections import Counter
    
    Counter(yourlist)
    
    5
    3
    5
    6
    from collections import Counter
    
    Counter(yourlist)
    
    5
    from collections import Counter
    
    Counter(yourlist)
    
    8
    3
    5
    9
    4
    0
    4
    1
    4
    2

    4
    8
    4
    9

    collections.Counter1import0

    Output:

    4

    220221from collections import Counter Counter(yourlist) 2 ________ 43 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;np.count_nonzero() function

    22
    0
    22
    5
    22
    6
    22
    7
    22
    8
    function counts the number of non-zero values in the array arr.

    22
    9NaN0 NaN1 NaN2 numpy.count_nonzero(arr, axis=None)

    NaN3

    22
    1NaN55____12 ________ 14 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;
    arr : [array_like] The array for which to count non-zeros.
    axis : [int or tuple, optional] Axis or tuple of axes along which to count non-zeros. Default is None, meaning that non-zeros will be counted along a flattened version of arr.

    22
    0NaN9 count[int or array of int] Number of non-zero values in the array along a given axis. Otherwise, the total number of non-zero values in the array is returned.

    Python3

    import

    from collections import Counter
    
    Counter(yourlist)
    
    0

    from collections import Counter
    
    Counter(yourlist)
    
    1
    from collections import Counter
    
    Counter(yourlist)
    
    2
    from collections import Counter
    
    Counter(yourlist)
    
    3
    from collections import Counter
    
    Counter(yourlist)
    
    4
    from collections import Counter
    
    Counter(yourlist)
    
    5
    from collections import Counter
    
    Counter(yourlist)
    
    6
    from collections import Counter
    
    Counter(yourlist)
    
    5
    from collections import Counter
    
    Counter(yourlist)
    
    8
    from collections import Counter
    
    Counter(yourlist)
    
    9
    3
    5
    0

    from collections import Counter
    
    Counter(yourlist)
    
    00
    4
    5
    from collections import Counter
    
    Counter(yourlist)
    
    02
    from collections import Counter
    
    Counter(yourlist)
    
    03

    4
    3count8
    from collections import Counter
    
    Counter(yourlist)
    
    06
    from collections import Counter
    
    Counter(yourlist)
    
    07

    from collections import Counter
    
    Counter(yourlist)
    
    00
    from collections import Counter
    
    Counter(yourlist)
    
    09
    from collections import Counter
    
    Counter(yourlist)
    
    5
    from collections import Counter
    
    Counter(yourlist)
    
    11
    from collections import Counter
    
    Counter(yourlist)
    
    03

    4
    3
    from collections import Counter
    
    Counter(yourlist)
    
    14
    from collections import Counter
    
    Counter(yourlist)
    
    15
    4
    0
    from collections import Counter
    
    Counter(yourlist)
    
    17
    from collections import Counter
    
    Counter(yourlist)
    
    07

    from collections import Counter
    
    Counter(yourlist)
    
    19
    from collections import Counter
    
    Counter(yourlist)
    
    20
    from collections import Counter
    
    Counter(yourlist)
    
    5
    from collections import Counter
    
    Counter(yourlist)
    
    22
    from collections import Counter
    
    Counter(yourlist)
    
    23
    from collections import Counter
    
    Counter(yourlist)
    
    24
    from collections import Counter
    
    Counter(yourlist)
    
    03

    4
    3
    from collections import Counter
    
    Counter(yourlist)
    
    14
    from collections import Counter
    
    Counter(yourlist)
    
    28
    from collections import Counter
    
    Counter(yourlist)
    
    29
    from collections import Counter
    
    Counter(yourlist)
    
    30
    from collections import Counter
    
    Counter(yourlist)
    
    07

    from collections import Counter
    
    Counter(yourlist)
    
    19
    from collections import Counter
    
    Counter(yourlist)
    
    33
    from collections import Counter
    
    Counter(yourlist)
    
    5
    from collections import Counter
    
    Counter(yourlist)
    
    35
    from collections import Counter
    
    Counter(yourlist)
    
    5
    from collections import Counter
    
    Counter(yourlist)
    
    37
    from collections import Counter
    
    Counter(yourlist)
    
    03

    3
    5
    1
    from collections import Counter
    
    Counter(yourlist)
    
    2
    from collections import Counter
    
    Counter(yourlist)
    
    3
    from collections import Counter
    
    Counter(yourlist)
    
    4
    from collections import Counter
    
    Counter(yourlist)
    
    5
    3
    5
    6
    from collections import Counter
    
    Counter(yourlist)
    
    5
    from collections import Counter
    
    Counter(yourlist)
    
    8
    3
    5
    9
    4
    0
    4
    1
    4
    2

    from collections import Counter
    
    Counter(yourlist)
    
    00
    from collections import Counter
    
    Counter(yourlist)
    
    48
    from collections import Counter
    
    Counter(yourlist)
    
    5
    from collections import Counter
    
    Counter(yourlist)
    
    50
    from collections import Counter
    
    Counter(yourlist)
    
    5
    from collections import Counter
    
    Counter(yourlist)
    
    52yourlist.count('Nan')1

    4
    8
    4
    9

    22
    0
    22
    1
    from collections import Counter
    
    Counter(yourlist)
    
    2 ________ 43 & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp; & nbsp;

    collections.Counter1

    from collections import Counter
    
    Counter(yourlist)
    
    62

    Output:

    22

    Làm thế nào để bạn đếm các giá trị nan trong danh sách python?

    Bạn có thể sử dụng cú pháp sau để đếm các giá trị NAN trong Pandas DataFrame:..
    .
    .
    .

    Làm thế nào để bạn đếm các giá trị nan?

    Chúng ta có thể sử dụng phương thức mô tả () trả về một bảng chứa chi tiết về bộ dữ liệu.Thuộc tính đếm trực tiếp cung cấp số lượng của các giá trị không phải NAN trong mỗi cột.Vì vậy, chúng ta có thể nhận được số lượng của các giá trị NAN, nếu chúng ta biết tổng số quan sát.

    Làm cách nào để đếm nan trong numpy?

    Để đếm số lượng các phiên bản NAN trong bộ dữ liệu, chúng ta có thể gọi NP.isnan để trả về mặt nạ đúng / sai tùy thuộc vào việc dữ liệu là NAN. Sau đó, chúng ta có thể sử dụng chức năng NP.Count_Nonzero để tổng hợp tổng số.call np. isnan to return a mask of true / false depending on whether the data is nan. Then we can use the np. count_nonzero function to sum up the total.

    Làm thế nào để bạn đếm các mục trong một danh sách trong Python?

    Chúng ta có thể sử dụng hàm Len () để trả về số lượng các phần tử có trong danh sách.use the len( ) function to return the number of elements present in the list.