1
$\begingroup$

I am using groupby.count for 2 columns to get value occurrences under a class constraint. However, if value $x$ in feature never occurs with class $y$, then this pandas method returns only non-zero frequencies. Is there any solution or alternate method?

The script is like:

combine = np.vstack([X_train[:,-1], y_train]).T
combine_df = pd.DataFrame(combine, columns = ['feature','class'])
class_count_groupby = combine_df[combine_df['class'] == 2]['class'].groupby(combine_df['feature']).count()
$\endgroup$
0
$\begingroup$

Use crosstab instead:

pd.crosstab(combine_df.class, combine_df.feature)
$\endgroup$
1
  • $\begingroup$ Thanks, this worked perfectly for my task. $\endgroup$ – Şafak Bilici May 14 at 17:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.