JukeboxHero
JukeboxHero

Reputation: 115

pandas dataframe group by values of a column

I have the following dataframe:

id expNumber attempt finished successful
1 1 1 true false
1 1 2 false false
1 1 3 true true
1 2 1 false false
1 2 2 true false
1 2 3 true true
1 4 1 false false
1 4 2 false false
id,expNumber,attempt,finished,successful,
1,1,1,true,false,
1,1,2,false,false,
1,1,3,true,true,
1,2,1,false,false,
1,2,2,true,false,
1,2,3,true,true,
1,4,1,false,false,
1,4,2,false,false,

And I want to create a dataframe which counts the data grouped by the value of the column. Here is what I expect:

true 4
false 4

I tried the following code but when I print the result, its just empty.

df = df.groupby('finished'.sum())
print("test", df)

Upvotes: 0

Views: 40

Answers (2)

jezrael
jezrael

Reputation: 862641

Select columns for count and use value_counts:

df = df[['finished','successful']].apply(pd.value_counts)
print("test", df)
test        finished  successful
False         4           6
True          4           2

Upvotes: 1

Borja_042
Borja_042

Reputation: 1071

You can do either of the following:

df.groupby("finished").count()

Or:

df["finished"].value_counts()

Upvotes: 1

Related Questions