【问题标题】:Why do I get an error on a Python Pandas dtype float64?为什么我在 Python Pandas dtype float64 上出现错误?
【发布时间】:2020-10-11 10:09:39
【问题描述】:

我在 Windows PC 上的 jupyter 笔记本中。我读过一个我称之为 tran 的数据框,如下所示

tran = pd.read_csv("https://raw.githubusercontent.com/m1ngle/TRCount/main/TRCountUS.csv")

当我查看整个数据框的数据类型时,效果很好

tran.dtypes

FIPS             int64
State           object
YMTF             int64
MTFPer         float64
YFTM             int64
FTMPer         float64
YNB              int64
NBPer          float64
YTR              int64
YTRper         float64
NoTR             int64
NoTRPer        float64
DK               int64
DKPer          float64
DNAns            int64
DNAPer         float64
TotSurveyed      int64
StatePop         int64
TRPop            int64
dtype: object

当我尝试调用或使用任何 float64 列时出现错误

tran['MTFPer'].dtype
KeyError                                  Traceback (most recent call last)
~\anaconda3\lib\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: 'MTFPer'

During handling of the above exception, another exception occurred:

KeyError                                  Traceback (most recent call last)
<ipython-input-85-2bd9c012f223> in <module>
----> 1 tran['MTFPer'].dtype

~\anaconda3\lib\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]

~\anaconda3\lib\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: 'MTFPer'

当我使用任何 int64 数据类型时,不会发生此错误。

tran['YMTF'].dtype
dtype('int64')

谁能帮帮我?

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    在读取.csv 文件时,pandas 还读取了与列一起出现的空白。 当我使用tran[' MTFPer '].dtype 而不是tran['MTFPer'].dtype 时,pandas 给了我正确的答案。
    也许清理一下数据本身,或者你可以像这样清理列名:

    tran.columns = [c.strip() for c in tran.columns]
    

    【讨论】:

    • 就是这样!该死,我已经尝试过 l.strip 我应该完成下一个合乎逻辑的步骤并剥离所有内容。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-10-10
    • 2020-04-02
    • 2019-07-28
    • 1970-01-01
    • 1970-01-01
    • 2017-06-09
    • 2020-09-15
    相关资源
    最近更新 更多