I have a multiindex dataframe and I cannot display my several graph blocks with one loop nested inside another. I would like to know how to go about it because I have tried everything and it still doesn't work. Only the first block of graphics is displayed but after that an error is generated. Here is my code:
i = 1
for question in questions:
plt.figure(figsize=(15,8))
plt.suptitle(question)
for prof in profs:
section = data.loc(axis=1)[prof,question].value_counts(normalize=True).sort_index(ascending=True).mul(100).round(1)
sns.set_theme()
plt.subplot(4, 4, i)
plt.title(prof, fontsize = 10)
plt.xlabel("Réponses des étudiants")
plt.ylabel('Effectif')
plt.tight_layout(pad=1.0)
ax = sns.barplot(x = section.keys(),y = section.values)
for j, v in enumerate(section.values):
ax.text(j, v/2, str(v)+"%", ha='center', color='black')
i = i+1
And here is the error that is generated:
KeyError Traceback (most recent call last)
Cell In[98], line 6
4 plt.suptitle(question)
5 for prof in profs:
----> 6 section = data.loc(axis=1)[prof,question].value_counts(normalize=True).sort_index(ascending=True).mul(100).round(1)
7 sns.set_theme()
8 plt.subplot(4, 4, i)
File c:\Users\Dell\anaconda3\Lib\site-packages\pandas\core\indexing.py:1067, in _LocationIndexer.__getitem__(self, key)
1065 if self._is_scalar_access(key):
1066 return self.obj._get_value(*key, takeable=self._takeable)
-> 1067 return self._getitem_tuple(key)
1068 else:
1069 # we by definition only have the 0th axis
1070 axis = self.axis or 0
File c:\Users\Dell\anaconda3\Lib\site-packages\pandas\core\indexing.py:1247, in _LocIndexer._getitem_tuple(self, tup)
1245 with suppress(IndexingError):
1246 tup = self._expand_ellipsis(tup)
-> 1247 return self._getitem_lowerdim(tup)
1249 # no multi-index, so validate all of the indexers
1250 tup = self._validate_tuple_indexer(tup)
File c:\Users\Dell\anaconda3\Lib\site-packages\pandas\core\indexing.py:937, in _LocationIndexer._getitem_lowerdim(self, tup)
...
File pandas\_libs\hashtable_class_helper.pxi:1920, in pandas._libs.hashtable.UInt64HashTable.get_item()
File pandas\_libs\hashtable_class_helper.pxi:1930, in pandas._libs.hashtable.UInt64HashTable.get_item()
KeyError: 353