0

I had a similar doubt as this category ordering issue. In this kernel Allstate EDA Skip to the part where he makes count plots of the categorical features, see that the order of A and B changes for cat2 and cat11 and many others. In this case however, we do not know how many unique categories are there, as for the later rows there are a lot more. Is there a easy way to fix it to have same order without writing a complex loop to run over a dictionary of the possible categories? EDIT: Since the dataset is huge, I have no idea how I can make it reproducible here. But to make it a bit clear, the code uses a loop

for i in range(n_rows):
    fg,ax = plt.subplots(nrows=1,ncols=n_cols,sharey=True,figsize=(12, 8))
    for j in range(n_cols):
        sns.countplot(x=cols[i*n_cols+j], data=dataset, ax=ax[j])

where n_cols = 4 and n_rows = 29.

The problem is that, as far as I know, we give a list or series to order. eg. order = ['A', 'B']. But in this dataset, for some columns there are only 2 categories A and B, but for others there are a lot of categories and different number of categories for each column.

(I feel like this is going nowhere)

deadcode
  • 742
  • 11
  • 27
  • Ordering should be pretty straight forward with seaborn, just as explained in the answer of the linked question, so I do not understand the problem. Please make the problem reproducible inside the question itself. See [mcve]. – ImportanceOfBeingErnest Jan 27 '18 at 22:16
  • @ImportanceOfBeingErnest Added an edit. Tell me if it is still not clear, I will take it down. – deadcode Jan 27 '18 at 22:33
  • 2
    Creating a [mcve] is for sure not easy. It does not need to contain a lot of datapoints though, also see [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). Here you would probably set the order to something like `dataset[cols[i*n_cols+j]].unique()`. – ImportanceOfBeingErnest Jan 27 '18 at 22:39
  • Didn't know about .unique(). I think that answers it. Thanks! – deadcode Jan 27 '18 at 22:43

0 Answers0