Heavy Traffic I-94 Barh

Screen Link: Learn data science with Python and R projects

My Code:

by_weather_main = traffic.groupby('weather_main').mean()
by_weather_description = traffic.groupby('weather_description').mean()
plt.barh(by_weather_main['weather_main'], by_weather_main['traffic_volume'])
plt.show()

What I expected to happen:
barh chart

What actually happened:

KeyError Traceback (most recent call last)
/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2645 try:
→ 2646 return self._engine.get_loc(key)
2647 except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ‘weather_main’

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last)
in
1 by_weather_main = traffic.groupby(‘weather_main’).mean()
2 by_weather_description = traffic.groupby(‘weather_description’).mean()
----> 3 plt.barh(by_weather_main[‘weather_main’], by_weather_main[‘traffic_volume’])
4 plt.show()

/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/frame.py in getitem(self, key)
2798 if self.columns.nlevels > 1:
2799 return self._getitem_multilevel(key)
→ 2800 indexer = self.columns.get_loc(key)
2801 if is_integer(indexer):
2802 indexer = [indexer]

/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2646 return self._engine.get_loc(key)
2647 except KeyError:
→ 2648 return self._engine.get_loc(self._maybe_cast_indexer(key))
2649 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2650 if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ‘weather_main’

plt.barh(by_weather_main[‘weather_main’], by_weather_main[‘traffic_volume’])
plt.show()
plt.barh(by_weather_main[‘weather_main’], by_weather_main[‘traffic_volume’])
plt.show()

KeyError Traceback (most recent call last)
/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2645 try:
→ 2646 return self._engine.get_loc(key)
2647 except KeyError:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ‘weather_main’

During handling of the above exception, another exception occurred:

KeyError Traceback (most recent call last)
in
----> 1 plt.barh(x=by_weather_main[‘weather_main’], y=by_weather_main[‘traffic_volume’])
2 plt.show()

/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/frame.py in getitem(self, key)
2798 if self.columns.nlevels > 1:
2799 return self._getitem_multilevel(key)
→ 2800 indexer = self.columns.get_loc(key)
2801 if is_integer(indexer):
2802 indexer = [indexer]

/dataquest/system/env/python3/lib/python3.8/site-packages/pandas/core/indexes/base.py in get_loc(self, key, method, tolerance)
2646 return self._engine.get_loc(key)
2647 except KeyError:
→ 2648 return self._engine.get_loc(self._maybe_cast_indexer(key))
2649 indexer = self.get_indexer([key], method=method, tolerance=tolerance)
2650 if indexer.ndim > 1 or indexer.size > 1:

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.PyObjectHashTable.get_item()

KeyError: ‘weather_main’

I haven’t completed this mission so I cannot experiment with the code to figure this out explicitly but it appears the error you’re getting is quite descriptive. It is saying that there is a KeyError with respect to weather_main. It means that the by_weather_main groupby object doesn’t have weather_main as a key.

If we think about how groupby works, this makes sense. Grouping our data by the weather_main column means that by_weather_main will not have this as a column anymore…the values found in weather_main will be used to group the data and will become a row of data in by_weather_mean.