Hướng dẫn how do you count nan values in a list python? - làm thế nào để bạn đếm các giá trị nan trong một danh sách python?

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 how do you count nan values in a list python? - làm thế nào để bạn đếm các giá trị nan trong một danh sách python?

Mark Amery

133K78 Huy hiệu vàng395 Huy hiệu bạc444 Huy hiệu đồng78 gold badges395 silver badges444 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 how do you count nan values in a list python? - làm thế nào để bạn đếm các giá trị nan trong một 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.56316 huy hiệu bạc31 huy hiệu đồng16 silver badges31 bronze badges

6

Is

2
6
6
3
from collections import Counter

Counter(yourlist)
7
from collections import Counter

Counter(yourlist)
8
from collections import Counter

Counter(yourlist)
39

Đếm NAN trong toàn bộ khung dữ liệu: Để đếm NAN trong toàn bộ tập dữ liệu, chúng ta chỉ cần gọi hàm SUM () hai lần - một lần để nhận số lượng trong mỗi cột và một lần nữa để tìm tổng số của tất cả các cột.

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

    2
    6
    6
    3
    from collections import Counter
    
    Counter(yourlist)
    
    7
    from collections import Counter
    
    Counter(yourlist)
    
    8
    from collections import Counter
    
    Counter(yourlist)
    
    39

    Đếm NAN trong toàn bộ khung dữ liệu: Để đếm NAN trong toàn bộ tập dữ liệu, chúng ta chỉ cần gọi hàm SUM () hai lần - một lần để nhận số lượng trong mỗi cột và một lần nữa để tìm tổng số của tất cả các cột.

    Is

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

    Bạn có thể sử dụng cú pháp sau để đếm các giá trị NAN trong Pandas DataFrame:.describe() method which returns a table containing details about the dataset. The count property directly gives the count of non-NaN values in each column. So, we can get the count of NaN values, if we know the total number of observations.

    import

    from collections import Counter
    
    Counter(yourlist)
    
    0

    import

    from collections import Counter
    
    Counter(yourlist)
    
    2

    Is

    2
    6
    2
    7
    2
    8
    2
    9
    from collections import Counter
    
    Counter(yourlist)
    
    9
    1
    1
    1
    2

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

    2
    6
    6
    3
    from collections import Counter
    
    Counter(yourlist)
    
    7
    from collections import Counter
    
    Counter(yourlist)
    
    8
    6
    6

    6
    7
    from collections import Counter
    
    Counter(yourlist)
    
    4
    6
    9
    from collections import Counter
    
    Counter(yourlist)
    
    3NaN1

    NaN2

    Đầu ra:

    Hướng dẫn how do you count nan values in a list python? - làm thế nào để bạn đếm các giá trị nan trong một danh sách python?

    Phương thức 2: Sử dụng hàm sum () hàm isnull () trả về một tập dữ liệu chứa các giá trị đúng và sai. Vì, true được coi là 1 và false là 0, hãy gọi phương thức SUM () trên chuỗi isnull () trả về số lượng các giá trị thực thực sự tương ứng với số lượng giá trị NAN.
    The isnull() function returns a dataset containing True and False values. Since, True is treated as a 1 and False as 0, calling the sum() method on the isnull() series returns the count of True values which actually corresponds to the number of NaN values.

    Đếm nan trong một cột:

    Chúng ta chỉ có thể tìm thấy các giá trị null trong cột mong muốn, sau đó lấy tổng.

    import NaN4

    import

    from collections import Counter
    
    Counter(yourlist)
    
    2

    Is

    2
    6
    2
    7
    2
    8
    2
    9
    from collections import Counter
    
    Counter(yourlist)
    
    9
    1
    1
    1
    2

    Các

    2
    6
    6
    3
    from collections import Counter
    
    Counter(yourlist)
    
    7
    from collections import Counter
    
    Counter(yourlist)
    
    8
    6
    6

    6
    7
    from collections import Counter
    
    Counter(yourlist)
    
    4
    6
    9
    from collections import Counter
    
    Counter(yourlist)
    
    3NaN1

    import6import7

    2
    7import9
    from collections import Counter
    
    Counter(yourlist)
    
    00
    from collections import Counter
    
    Counter(yourlist)
    
    01

    Đầu ra:

    2

    Đếm nan liên tiếp:

    Hàng có thể được chọn bằng LỘC hoặc ILOC. Sau đó, chúng tôi tìm thấy tổng như trước đây.

    import

    from collections import Counter
    
    Counter(yourlist)
    
    0

    import

    from collections import Counter
    
    Counter(yourlist)
    
    2

    Is

    2
    6
    2
    7
    2
    8
    2
    9
    from collections import Counter
    
    Counter(yourlist)
    
    9
    1
    1
    1
    2

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

    Các

    6
    7
    from collections import Counter
    
    Counter(yourlist)
    
    4
    6
    9
    from collections import Counter
    
    Counter(yourlist)
    
    3NaN1

    import6

    from collections import Counter
    
    Counter(yourlist)
    
    46
    from collections import Counter
    
    Counter(yourlist)
    
    8
    from collections import Counter
    
    Counter(yourlist)
    
    48
    from collections import Counter
    
    Counter(yourlist)
    
    00
    from collections import Counter
    
    Counter(yourlist)
    
    01

    Đầu ra:

    1

    Đếm nan liên tiếp:
    To count NaN in the entire dataset, we just need to call the sum() function twice – once for getting the count in each column and again for finding the total sum of all the columns.

    import

    from collections import Counter
    
    Counter(yourlist)
    
    0

    import

    from collections import Counter
    
    Counter(yourlist)
    
    2

    Is

    2
    6
    2
    7
    2
    8
    2
    9
    from collections import Counter
    
    Counter(yourlist)
    
    9
    1
    1
    from collections import Counter
    
    Counter(yourlist)
    
    74

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

    2
    6
    6
    3
    from collections import Counter
    
    Counter(yourlist)
    
    7
    from collections import Counter
    
    Counter(yourlist)
    
    8
    from collections import Counter
    
    Counter(yourlist)
    
    88

    6
    7
    from collections import Counter
    
    Counter(yourlist)
    
    4
    6
    9
    from collections import Counter
    
    Counter(yourlist)
    
    3NaN1

    import6

    from collections import Counter
    
    Counter(yourlist)
    
    95
    from collections import Counter
    
    Counter(yourlist)
    
    00
    from collections import Counter
    
    Counter(yourlist)
    
    97
    from collections import Counter
    
    Counter(yourlist)
    
    00
    from collections import Counter
    
    Counter(yourlist)
    
    01

    Đầu ra:

    6

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

    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 isnull trong Python?

    Vì sum () tính toán là true = 1 và false = 0, bạn có thể đếm số lượng các giá trị bị thiếu trong mỗi hàng và cột bằng cách gọi sum () từ kết quả của isnull ().Bạn có thể đếm các giá trị bị thiếu trong mỗi cột theo mặc định và trong mỗi hàng có trục = 1.by calling sum() from the result of isnull() . You can count missing values in each column by default, and in each row with axis=1 .