무회blog

python: 파이썬 특정 문자 개수 세어 보기 본문

Python

python: 파이썬 특정 문자 개수 세어 보기

최무회 2020. 4. 26. 04:01

-사용코드

txt = 'Show me the money.'
word_1 = txt.count('S')
word_2 = txt.count('o')
word_3 = txt.count('m')

print(word_1)
print(word_2)
print(word_3)



Series 의 행 개수를 셀 때 s.size 와 같이 뒤에 () 가 없다는 것 조심해야 겠습니다.그리고 count()는 Null 값이 아닌 행(count Non-null rows)만 세며,

> size() 는 Null 값인 행도 모두 포함해서 행(size of all rows)을 센다

는 것도 유념하면 좋겠습니다. 

 

구분

pandas DataFrame (df)

pandas Series (s)

행 개수 세기

(row count)

len(df)

df.shape[0]

len(df.index)

len(s)

s.size

len(s.index)

열 개수 세기

(column count)

df.shape[1]

len(df.columns)

N/A

Null 값이 아닌 행 개수 세기

(Non-null row count)

df.count()

s.count()

그룹 별 행 개수 세기

(Row count per group)

df.groupby(...).size()

s.groupby(...).size()

그룹 별 Null 값이 아닌 행 개수 세기

(Non-null row count per group)

df.groupby(...).count()

s.groupby(...).count()

 

 

간단한 예제를 아래에 소개합니다. 

numpy와 pandas 라이브러리 불러오고, DataFrame과 Series 데이터셋 만들어보겠습니다. 




Comments