3

In pandas crosstab, now I am getting the output as below if the other col contains all zero value:

  0
0 5
1 2

But I need to get an output for the other column even if it contains all zero.

  0 1
0 5 0
1 2 0

I am using below code to create cross tab:

data_crosstab = pd.crosstab(data[df_all.columns[56]],
                                    data[df_all.columns[57]], 
                                       margins = False,dropna=False)

1 Answer 1

4

Use DataFrame.reindex:

#margins=False is default value, so removed
#https://pandas.pydata.org/docs/reference/api/pandas.crosstab.html
data_crosstab = pd.crosstab(data[df_all.columns[56]],
                            data[df_all.columns[57]], dropna=False)

data_crosstab = data_crosstab.reindex(columns=[0,1], fill_value=0)

More general solution:

data_crosstab = data_crosstab.reindex(columns=[0,1],index=[0,1], fill_value=0)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.