Home > Software engineering >  How to count occurrences per label?
How to count occurrences per label?

Time:09-15

How can I count the 1's and 0's in column [1] per unique class in column [0]?

labels = []

estimator = est.fit(X.iloc[:,1:])
labels.append(estimator.labels_)
labels.append(O)
labels = pd.DataFrame(np.array(labels).transpose())
labels.iloc[:,1] = (labels.iloc[:,1] > 5).astype(int) # Binary GOS-E

x = np.array(labels.iloc[:,1]).reshape(-1, 1)
y = np.array(labels.iloc[:,0])

CodePudding user response:

df.groupby may suit your needs.

Group by class in column 0, and aggregate using 'sum', which works because only 1s will be 'counted'

df.groupby(df.iloc[:,0]).agg('sum') or df.groupby(df.iloc[:,0]).sum() should work. You can also use df['name of column 0'] instead of df.iloc[:,0].

  • Related