codememo

팬더에서 열에서 False 또는 True가 발생한 횟수

tipmemo 2023. 10. 30. 21:01
반응형

팬더에서 열에서 False 또는 True가 발생한 횟수

정해진

patient_id  test_result has_cancer
0   79452   Negative    False
1   81667   Positive    True
2   76297   Negative    False
3   36593   Negative    False
4   53717   Negative    False
5   67134   Negative    False
6   40436   Negative    False

파이썬에서 열에서 거짓 또는 참을 세는 방법?

노력해왔었는데요.

# number of patients with cancer

number_of_patients_with_cancer= (df["has_cancer"]==True).count()
print(number_of_patients_with_cancer)

그래서 당신은 필요합니다.value_counts?

df.col_name.value_counts()
Out[345]: 
False    6
True     1
Name: has_cancer, dtype: int64

한다면has_cancerNaNs:

false_count = (~df.has_cancer).sum()

한다면has_cancerNaN을 포함하지 않으며, 데이터 프레임의 길이에서 차감하여 부정을 방지하는 것도 방법입니다.이전 접근 방식보다 반드시 개선된 것은 아닙니다.

false_count = len(df) - df.has_cancer.sum()

마찬가지로 True 값의 개수만 세고 싶다면 다음과 같습니다.

true_count = df.has_cancer.sum()

둘 다 원하신다면.

fc, tc = df.has_cancer.value_counts().sort_index().tolist()
0     True
1    False
2    False
3    False
4    False
5    False
6    False
7    False
8    False
9    False

위의 팬더 시리즈를 예라고 한다면,

example.sum()

그러면 이 코드는 하나밖에 없기 때문에 1을 출력합니다.True시리즈의 value입니다.카운트를 가져오려면 다음과 같이 하십시오.False

len(example) - example.sum()
number_of_patients_with_cancer = df.has_cancer[df.has_cancer==True].count()

위의 데이터 프레임을 df로 간주합니다.

True_Count = df[df.has_cancer == True]

len(True_Count)

열을 합하면 참값을 셀 수 있습니다.False는 0의 특수한 경우이고 True는 1의 특수한 경우일 뿐입니다.거짓 수는 행 수에서 그것을 뺀 것입니다.당신이 가지고 있지 않다면.na그 안에 있습니다.

세어보세요True:

df["has_cancer"].sum()

세어보세요False:

(~df["has_cancer"]).sum()

부울 연산자 참조.

언급URL : https://stackoverflow.com/questions/53550988/count-occurrences-of-false-or-true-in-a-column-in-pandas

반응형