Home > front end >  pandas dataframe group by values of a column
pandas dataframe group by values of a column

Time:10-26

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)

CodePudding user response:

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

CodePudding user response:

You can do either of the following:

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

Or:

df["finished"].value_counts()
  • Related