简体   繁体   English

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

[英]Pandas read_csv - non-printable character (columns not recognized)

Could someone tell me what non-printable character I have in my code that makes python not recognize the columns names in my dataframe ?有人能告诉我我的代码中有什么不可打印的字符使 python 无法识别我的数据框中的列名吗? :

import pandas as pd将熊猫导入为 pd

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

Here is the Traceback of the error when I try to group by teamname :这是我尝试按 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

Thank you very much !非常感谢 !

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.在使用 Pandas 时,情况大致相同。 .loc is trying to locate a key value that is not found in the data frame. .loc正在尝试定位在数据框中找不到的键值。

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.查看您的代码和回溯错误,我的假设是因为您正在尝试查找winter_games_bronze_won (开头有空格),因此您收到了错误。 Try removing the spaces before winter_games_bronze_won and see what happens.尝试删除winter_games_bronze_won之前的空格,看看会发生什么。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM