Pandasでカウントを取得する方法(count)

投稿者: | 2020年11月6日

Pandasでカウントを取得する方法(count)です

csvファイル

1,2,3,4,5,6,7,8,9,10
2,2,3,4,5,6,7,8,9,10
3,2,3,4,5,6,7,8,9,10
4,2,3,4,5,6,7,8,9,10
5,52,53,54,55,56,57,58,59,50
6,2,3,4,5,6,7,8,9,10
7,2,3,4,5,6,7,8,9,10
8,2,3,4,5,6,7,8,9,10
9,2,3,4,5,6,7,8,9,10
10,2,3,4,5,6,7,8,9,10

a列のカウント取得

import pandas as pd

file = r'csvファイルのパス'
df = pd.read_csv(file, header=None, names=('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'))
print(df)
print(df['a'].count())

結果

    a   b   c   d   e   f   g   h   i   j
0   1   2   3   4   5   6   7   8   9  10
1   2   2   3   4   5   6   7   8   9  10
2   3   2   3   4   5   6   7   8   9  10
3   4   2   3   4   5   6   7   8   9  10
4   5  52  53  54  55  56  57  58  59  50
5   6   2   3   4   5   6   7   8   9  10
6   7   2   3   4   5   6   7   8   9  10
7   8   2   3   4   5   6   7   8   9  10
8   9   2   3   4   5   6   7   8   9  10
9  10   2   3   4   5   6   7   8   9  10
10 ← df['a'].count()の結果

a列が5のカウント取得

df[df['a']==5].count()

結果

a    1
b    1
c    1
d    1
e    1
f    1
g    1
h    1
i    1
j    1
dtype: int64