0

Could someone tell me what non-printable character I have in my code that makes python not recognize the columns names in my dataframe? :

import pandas as pd

data_olymp = pd.read_csv("Olympics_data.csv", sep=";")

Here is the Traceback of the error when I try to group by teamname :


---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-103-ae95f10f5210> in <module>
     30 # print(type(réponse1))
     31 # print(len(winter_games_bronze_won))
---> 32 print(data_olymp.loc[" winter_games_bronze_won"] == 9)

~\anaconda3\lib\site-packages\pandas\core\indexing.py in __getitem__(self, key)
    893 
    894             maybe_callable = com.apply_if_callable(key, self.obj)
--> 895             return self._getitem_axis(maybe_callable, axis=axis)
    896 
    897     def _is_scalar_access(self, key: Tuple):

~\anaconda3\lib\site-packages\pandas\core\indexing.py in _getitem_axis(self, key, axis)
   1122         # fall thru to straight lookup
   1123         self._validate_key(key, axis)
-> 1124         return self._get_label(key, axis=axis)
   1125 
   1126     def _get_slice_axis(self, slice_obj: slice, axis: int):

~\anaconda3\lib\site-packages\pandas\core\indexing.py in _get_label(self, label, axis)
   1071     def _get_label(self, label, axis: int):
   1072         # GH#5667 this will fail if the label is not present in the axis.
-> 1073         return self.obj.xs(label, axis=axis)
   1074 
   1075     def _handle_lowerdim_multi_index_axis0(self, tup: Tuple):

~\anaconda3\lib\site-packages\pandas\core\generic.py in xs(self, key, axis, level, drop_level)
   3737                 raise TypeError(f"Expected label or tuple of labels, got {key}") from e
   3738         else:
-> 3739             loc = index.get_loc(key)
   3740 
   3741             if isinstance(loc, np.ndarray):

~\anaconda3\lib\site-packages\pandas\core\indexes\range.py in get_loc(self, key, method, tolerance)
    352                 except ValueError as err:
    353                     raise KeyError(key) from err
--> 354             raise KeyError(key)
    355         return super().get_loc(key, method=method, tolerance=tolerance)
    356 

KeyError: ' winter_games_bronze_won'

The file looks like that :

team_name; summer_games_played; summer_games_gold_won; summer_games_silver_won; summer_games_bronze_won; summer_games_medals_won; winter_games_played; winter_games_gold_won;  winter_games_silver_won; winter_games_bronze_won; winter_games_medals_won; total_games_played
Canada (CAN);13;0;0;2;2;0;0;0;0;0;13
United States (USA);12;5;2;8;15;3;0;0;0;0;15
Russia (RUS);23;18;24;28;70;18;0;0;0;0;41
Ruli
  • 2,592
  • 12
  • 30
  • 40
  • Actually I think the problem is that every element of date is seperated by ";" but at the end of each line there is no semi-colon. Is it why python doesn't recognize my column names ? – LostITStudent Nov 11 '21 at 17:05
  • There are no non-printable characters in your code. There are non-printable characters in your CSV file. Since you didn't show us either the error traceback nor the first few lines of the file, there is nothing anyone here can do for you. – Tim Roberts Nov 11 '21 at 18:21
  • @LostITStudent If you solved the issue, consider accepting the answer that helped you, and/or upvoting it. – Ruli Nov 11 '21 at 22:01

1 Answers1

0

Key errors are raised when you are trying to access a key that is not in a dictionary. While working Pandas, it is about the same thing. .loc is trying to locate a key value that is not found in the data frame.

Looking at your code and the traceback error, my assumption is that because you are trying to look up winter_games_bronze_won (with the spaces at the beginning), you are getting the error. Try removing the spaces before winter_games_bronze_won and see what happens.

Dylan Skinner
  • 26
  • 1
  • 4