Pandas read_csv - 不可打印字符(无法识别列)

Pandas read_csv - non-printable character (columns not recognized)

有人能告诉我我的代码中有什么不可打印的字符导致 python 无法识别我的数据框中的列名称吗? :

import pandas as pd

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

这是我尝试按团队名称分组时的错误回溯:


---------------------------------------------------------------------------
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'

文件看起来像这样:

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

当您尝试访问不在字典中的键时,会出现键错误。在工作Pandas的时候,也是差不多的事情。 .loc 正在尝试定位在数据框中找不到的键值。

查看您的代码和回溯错误,我的假设是因为您正在尝试查找 winter_games_bronze_won(开头有空格),所以您收到了错误。尝试删除 winter_games_bronze_won 之前的空格,看看会发生什么。