yilmaz Cam
yilmaz Cam

Reputation: 29

Python Panda : Count number of occurence of a number

I've searched for long time and I need your help, I'm newbie on python and panda lib. I've a dataframe like that charged from a csv file :

ball_1,ball_2,ball_3,ball_4,ball_5,ball_6,ball_7,extraball_1,extraball_2
10,32,25,5,8,19,21,3,4
43,12,8,19,4,37,12,1,5
12,16,43,19,4,28,40,2,4

ball_X is an int in between 1-50 and extraball_X is an int between 1-9. I want count how many times appear each number in 2 other frames like that : First DF ball :

Number,Score
1,128
2,34
3,12
4,200
....
50,145

Second DF extraball :

Number,Score
1,340
2,430
3,123
4,540
....
9,120

I've the algorythme in my head but i'm too noob in panda to translate into code. I Hope it's clear enough and someone will be able to help me. Dont hesitate if you have questions.

Upvotes: 1

Views: 61

Answers (1)

piRSquared
piRSquared

Reputation: 294218

groupby on columns with value_counts

def get_before_underscore(x):
    return x.split('_', 1)[0]

val_counts = {
    k: d.stack().value_counts()
    for k, d in df.groupby(get_before_underscore, axis=1)
}

print(val_counts['ball'])

12    3
19    3
4     2
8     2
43    2
32    1
5     1
10    1
37    1
40    1
16    1
21    1
25    1
28    1
dtype: int64

print(val_counts['extraball'])

4    2
1    1
2    1
3    1
5    1
dtype: int64

Upvotes: 3

Related Questions