Using map_dataframe in a FacetGrid

Thanks in advance for your help with my query. I would like to generate 4 heatmaps, the specialty is the row and colnum indicates the column in the facetgrid. Each heatmap should reflect the specialty and associated colnum. The final heatmap is called finalheatdf. I have more columns passed to sns.FacetGrid than I want for map_dataframe (Site on y axis and [‘0-2′,’11-12′,’3-4′,’5-6’] on the x axis). I am receiving an error ValueError: could not convert string to float: ‘X1’. My code is listed below

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import re
import numpy as np
waitremap = {'0-2':0,'3-4':1,'5-6':2,'7-8':3,'9-10':4,'11-12':5}
df = pd.DataFrame({ 'Spec':['A','A','A','B','B','B','A','B'],
'Wait':[5,6,2,4,1,2,11,12],
'Hosp':['X1','X2','X1','X2','X1','X2','X1','X2'],
'WaitClass':['5-6','5-6','0-2','3-4','0-2','0-2','11-12','11-12'],
#'specrow' :[1,1,1,2,2,2,1,2]
})
df = (df
.assign(waitindex = df.WaitClass.map(waitremap))
)
print(df)
heatdf = (pd.crosstab(index=[df.Hosp,df.Spec],columns=df.WaitClass,values=df.Wait,aggfunc='count',normalize='index')
.assign(colnum =1)
.reset_index()
)
print('********** heatdf *****************')
print(heatdf)
print('********** heatdf *****************')
heatdf1 = (pd.crosstab(index=[df.Hosp,df.Spec],columns=df.WaitClass,values=df.Wait,aggfunc='count',normalize=True)
.assign(colnum =2)
.reset_index()
)
finalheatdf = pd.concat([heatdf,heatdf1])
finalheatdf.index = finalheatdf.Hosp
print(finalheatdf)
print(finalheatdf.dtypes)
print(finalheatdf.index)
g3 = sns.FacetGrid(finalheatdf, col='colrow', row='Spec')
g3.map_dataframe(sns.heatmap)
</code>
<code>import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import re import numpy as np waitremap = {'0-2':0,'3-4':1,'5-6':2,'7-8':3,'9-10':4,'11-12':5} df = pd.DataFrame({ 'Spec':['A','A','A','B','B','B','A','B'], 'Wait':[5,6,2,4,1,2,11,12], 'Hosp':['X1','X2','X1','X2','X1','X2','X1','X2'], 'WaitClass':['5-6','5-6','0-2','3-4','0-2','0-2','11-12','11-12'], #'specrow' :[1,1,1,2,2,2,1,2] }) df = (df .assign(waitindex = df.WaitClass.map(waitremap)) ) print(df) heatdf = (pd.crosstab(index=[df.Hosp,df.Spec],columns=df.WaitClass,values=df.Wait,aggfunc='count',normalize='index') .assign(colnum =1) .reset_index() ) print('********** heatdf *****************') print(heatdf) print('********** heatdf *****************') heatdf1 = (pd.crosstab(index=[df.Hosp,df.Spec],columns=df.WaitClass,values=df.Wait,aggfunc='count',normalize=True) .assign(colnum =2) .reset_index() ) finalheatdf = pd.concat([heatdf,heatdf1]) finalheatdf.index = finalheatdf.Hosp print(finalheatdf) print(finalheatdf.dtypes) print(finalheatdf.index) g3 = sns.FacetGrid(finalheatdf, col='colrow', row='Spec') g3.map_dataframe(sns.heatmap) </code>
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import re
import numpy as np

waitremap = {'0-2':0,'3-4':1,'5-6':2,'7-8':3,'9-10':4,'11-12':5}


df = pd.DataFrame({ 'Spec':['A','A','A','B','B','B','A','B'],
                    'Wait':[5,6,2,4,1,2,11,12],
                   'Hosp':['X1','X2','X1','X2','X1','X2','X1','X2'],
                   'WaitClass':['5-6','5-6','0-2','3-4','0-2','0-2','11-12','11-12'],
                   #'specrow' :[1,1,1,2,2,2,1,2]
                  })

df = (df
     .assign(waitindex = df.WaitClass.map(waitremap))
     )

print(df)

heatdf = (pd.crosstab(index=[df.Hosp,df.Spec],columns=df.WaitClass,values=df.Wait,aggfunc='count',normalize='index')
          .assign(colnum =1)
          .reset_index()
         )



print('********** heatdf *****************')
print(heatdf)
print('********** heatdf *****************')

heatdf1 = (pd.crosstab(index=[df.Hosp,df.Spec],columns=df.WaitClass,values=df.Wait,aggfunc='count',normalize=True)
           .assign(colnum =2)
          .reset_index()
          )


finalheatdf = pd.concat([heatdf,heatdf1])
finalheatdf.index = finalheatdf.Hosp
print(finalheatdf)
print(finalheatdf.dtypes)
print(finalheatdf.index)
           
g3 = sns.FacetGrid(finalheatdf, col='colrow', row='Spec')
g3.map_dataframe(sns.heatmap)

Many Thanks

Steven

You can’t use sns.heatmap directly since it has the extra non-numeric columns, you should use a wrapper to only select the needed columns to pass to heatmap:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def heatmap(*args, **kwargs):
sns.heatmap(kwargs['data'].drop(columns=kwargs['drop'], errors='ignore'))
g3 = sns.FacetGrid(finalheatdf.drop(columns='Hosp'), col='colnum', row='Spec')
g3.map_dataframe(heatmap, drop=['colnum', 'Spec'])
</code>
<code>def heatmap(*args, **kwargs): sns.heatmap(kwargs['data'].drop(columns=kwargs['drop'], errors='ignore')) g3 = sns.FacetGrid(finalheatdf.drop(columns='Hosp'), col='colnum', row='Spec') g3.map_dataframe(heatmap, drop=['colnum', 'Spec']) </code>
def heatmap(*args, **kwargs):
    sns.heatmap(kwargs['data'].drop(columns=kwargs['drop'], errors='ignore'))

g3 = sns.FacetGrid(finalheatdf.drop(columns='Hosp'), col='colnum', row='Spec')
g3.map_dataframe(heatmap, drop=['colnum', 'Spec'])

Or pass a list of columns to keep:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def heatmap(*args, **kwargs):
sns.heatmap(kwargs['data'].reindex(columns=kwargs['cols']))
g3 = sns.FacetGrid(finalheatdf.drop(columns='Hosp'), col='colnum', row='Spec')
g3.map_dataframe(heatmap, cols=['0-2', '11-12', '3-4', '5-6'])
</code>
<code>def heatmap(*args, **kwargs): sns.heatmap(kwargs['data'].reindex(columns=kwargs['cols'])) g3 = sns.FacetGrid(finalheatdf.drop(columns='Hosp'), col='colnum', row='Spec') g3.map_dataframe(heatmap, cols=['0-2', '11-12', '3-4', '5-6']) </code>
def heatmap(*args, **kwargs):
    sns.heatmap(kwargs['data'].reindex(columns=kwargs['cols']))

g3 = sns.FacetGrid(finalheatdf.drop(columns='Hosp'), col='colnum', row='Spec')
g3.map_dataframe(heatmap, cols=['0-2', '11-12', '3-4', '5-6'])

NB. you could make a generic function that accepts either case.

Output:

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa

Using map_dataframe in a FacetGrid

Thanks in advance for your help with my query. I would like to generate 4 heatmaps, the specialty is the row and colnum indicates the column in the facetgrid. Each heatmap should reflect the specialty and associated colnum. The final heatmap is called finalheatdf. I have more columns passed to sns.FacetGrid than I want for map_dataframe (Site on y axis and [‘0-2′,’11-12′,’3-4′,’5-6’] on the x axis). I am receiving an error ValueError: could not convert string to float: ‘X1’. My code is listed below

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import re
import numpy as np
waitremap = {'0-2':0,'3-4':1,'5-6':2,'7-8':3,'9-10':4,'11-12':5}
df = pd.DataFrame({ 'Spec':['A','A','A','B','B','B','A','B'],
'Wait':[5,6,2,4,1,2,11,12],
'Hosp':['X1','X2','X1','X2','X1','X2','X1','X2'],
'WaitClass':['5-6','5-6','0-2','3-4','0-2','0-2','11-12','11-12'],
#'specrow' :[1,1,1,2,2,2,1,2]
})
df = (df
.assign(waitindex = df.WaitClass.map(waitremap))
)
print(df)
heatdf = (pd.crosstab(index=[df.Hosp,df.Spec],columns=df.WaitClass,values=df.Wait,aggfunc='count',normalize='index')
.assign(colnum =1)
.reset_index()
)
print('********** heatdf *****************')
print(heatdf)
print('********** heatdf *****************')
heatdf1 = (pd.crosstab(index=[df.Hosp,df.Spec],columns=df.WaitClass,values=df.Wait,aggfunc='count',normalize=True)
.assign(colnum =2)
.reset_index()
)
finalheatdf = pd.concat([heatdf,heatdf1])
finalheatdf.index = finalheatdf.Hosp
print(finalheatdf)
print(finalheatdf.dtypes)
print(finalheatdf.index)
g3 = sns.FacetGrid(finalheatdf, col='colrow', row='Spec')
g3.map_dataframe(sns.heatmap)
</code>
<code>import pandas as pd import seaborn as sns import matplotlib.pyplot as plt import re import numpy as np waitremap = {'0-2':0,'3-4':1,'5-6':2,'7-8':3,'9-10':4,'11-12':5} df = pd.DataFrame({ 'Spec':['A','A','A','B','B','B','A','B'], 'Wait':[5,6,2,4,1,2,11,12], 'Hosp':['X1','X2','X1','X2','X1','X2','X1','X2'], 'WaitClass':['5-6','5-6','0-2','3-4','0-2','0-2','11-12','11-12'], #'specrow' :[1,1,1,2,2,2,1,2] }) df = (df .assign(waitindex = df.WaitClass.map(waitremap)) ) print(df) heatdf = (pd.crosstab(index=[df.Hosp,df.Spec],columns=df.WaitClass,values=df.Wait,aggfunc='count',normalize='index') .assign(colnum =1) .reset_index() ) print('********** heatdf *****************') print(heatdf) print('********** heatdf *****************') heatdf1 = (pd.crosstab(index=[df.Hosp,df.Spec],columns=df.WaitClass,values=df.Wait,aggfunc='count',normalize=True) .assign(colnum =2) .reset_index() ) finalheatdf = pd.concat([heatdf,heatdf1]) finalheatdf.index = finalheatdf.Hosp print(finalheatdf) print(finalheatdf.dtypes) print(finalheatdf.index) g3 = sns.FacetGrid(finalheatdf, col='colrow', row='Spec') g3.map_dataframe(sns.heatmap) </code>
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
import re
import numpy as np

waitremap = {'0-2':0,'3-4':1,'5-6':2,'7-8':3,'9-10':4,'11-12':5}


df = pd.DataFrame({ 'Spec':['A','A','A','B','B','B','A','B'],
                    'Wait':[5,6,2,4,1,2,11,12],
                   'Hosp':['X1','X2','X1','X2','X1','X2','X1','X2'],
                   'WaitClass':['5-6','5-6','0-2','3-4','0-2','0-2','11-12','11-12'],
                   #'specrow' :[1,1,1,2,2,2,1,2]
                  })

df = (df
     .assign(waitindex = df.WaitClass.map(waitremap))
     )

print(df)

heatdf = (pd.crosstab(index=[df.Hosp,df.Spec],columns=df.WaitClass,values=df.Wait,aggfunc='count',normalize='index')
          .assign(colnum =1)
          .reset_index()
         )



print('********** heatdf *****************')
print(heatdf)
print('********** heatdf *****************')

heatdf1 = (pd.crosstab(index=[df.Hosp,df.Spec],columns=df.WaitClass,values=df.Wait,aggfunc='count',normalize=True)
           .assign(colnum =2)
          .reset_index()
          )


finalheatdf = pd.concat([heatdf,heatdf1])
finalheatdf.index = finalheatdf.Hosp
print(finalheatdf)
print(finalheatdf.dtypes)
print(finalheatdf.index)
           
g3 = sns.FacetGrid(finalheatdf, col='colrow', row='Spec')
g3.map_dataframe(sns.heatmap)

Many Thanks

Steven

You can’t use sns.heatmap directly since it has the extra non-numeric columns, you should use a wrapper to only select the needed columns to pass to heatmap:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def heatmap(*args, **kwargs):
sns.heatmap(kwargs['data'].drop(columns=kwargs['drop'], errors='ignore'))
g3 = sns.FacetGrid(finalheatdf.drop(columns='Hosp'), col='colnum', row='Spec')
g3.map_dataframe(heatmap, drop=['colnum', 'Spec'])
</code>
<code>def heatmap(*args, **kwargs): sns.heatmap(kwargs['data'].drop(columns=kwargs['drop'], errors='ignore')) g3 = sns.FacetGrid(finalheatdf.drop(columns='Hosp'), col='colnum', row='Spec') g3.map_dataframe(heatmap, drop=['colnum', 'Spec']) </code>
def heatmap(*args, **kwargs):
    sns.heatmap(kwargs['data'].drop(columns=kwargs['drop'], errors='ignore'))

g3 = sns.FacetGrid(finalheatdf.drop(columns='Hosp'), col='colnum', row='Spec')
g3.map_dataframe(heatmap, drop=['colnum', 'Spec'])

Or pass a list of columns to keep:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>def heatmap(*args, **kwargs):
sns.heatmap(kwargs['data'].reindex(columns=kwargs['cols']))
g3 = sns.FacetGrid(finalheatdf.drop(columns='Hosp'), col='colnum', row='Spec')
g3.map_dataframe(heatmap, cols=['0-2', '11-12', '3-4', '5-6'])
</code>
<code>def heatmap(*args, **kwargs): sns.heatmap(kwargs['data'].reindex(columns=kwargs['cols'])) g3 = sns.FacetGrid(finalheatdf.drop(columns='Hosp'), col='colnum', row='Spec') g3.map_dataframe(heatmap, cols=['0-2', '11-12', '3-4', '5-6']) </code>
def heatmap(*args, **kwargs):
    sns.heatmap(kwargs['data'].reindex(columns=kwargs['cols']))

g3 = sns.FacetGrid(finalheatdf.drop(columns='Hosp'), col='colnum', row='Spec')
g3.map_dataframe(heatmap, cols=['0-2', '11-12', '3-4', '5-6'])

NB. you could make a generic function that accepts either case.

Output:

1

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật