dartdog dartdog - 6 months ago 94
Python Question

Getting facet grid plot out of Pandas dataframe

So I have a dataframe that looks like: (1st col is just index)



CCS_Category_ICD9 Gender Admit_Month count
0 1 M 9 1
1 2 F 1 5
2 2 F 3 1
3 2 F 4 1
4 2 F 5 2
5 2 F 8 3
6 2 F 9 1
7 2 F 10 1
8 2 F 11 3
9 2 F 12 5
10 2 M 2 2
11 2 M 4 1
12 2 M 5 4
13 2 M 7 2
14 2 M 8 11
15 2 M 11 2
16 2 M 12 1
17 3 F 9 1
18 3 M 8 1
19 3 M 11 1
20 4 F 1 8
21 4 F 2 10
22 4 F 3 6
23 4 F 4 10
24 4 F 5 35
25 4 F 6 13
26 4 F 7 10
27 4 F 8 23
28 4 F 9 12
29 4 F 10 13
... ... ... ... ...
4758 2620 M 7 7





I have been trying to get a plot by ccs code and the count of by admit_month and gender using seaborn

This sort of gets in the right direction:

g = sns.factorplot(x="Admit_Month", y="count", column='CCS_Category_ICD9',data=ngrpdf)


Admit by month

But what I really want is a full set of charts one per ccs_category with the admit month as the bottom axis and bars indicating the counts by gender for the month

So I was trying something like:

g = sns.FacetGrid(ngrpdf, col='CCS_Category_ICD9',hue='CCS_Category_ICD9', col_wrap=4)
g.map(sns.factorplot(x="Admit_Month", y="count"))


Which just blows up, and nothing seems to get me bars by gender..
Been messing around sadly for a while with no luck. any guidance appreciated...

Answer

With many thanks to @mwaskom for the great work and pointer to read the facet grid doc more carefully, this is what I came up with:

pal= dict(M='blue',F='red')
g = sns.FacetGrid(ngrpdf, col='CCS_Category_ICD9',hue='Gender', palette=pal,hue_order=['M','F'], col_wrap=4)
g = (g.map(plt.scatter,"Admit_Month","count").add_legend())

Which works like a charm even with a 1 million record dataset and some 200+ ccs categories:

An except from the charts looks like:

enter image description here