为什么Python数据类型float64会出错?

2024-09-30 20:18:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我在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')

有人能帮我吗


Tags: keyinselfpandasgetindexloclibs
1条回答
网友
1楼 · 发布于 2024-09-30 20:18:10

在读取.csv文件时,pandas还会读取列中的空白。 当我用tran[' MTFPer '].dtype代替tran['MTFPer'].dtype时,熊猫给了我正确的答案。
也许可以稍微清理一下数据本身,或者可以像这样清理列名:

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

相关问题 更多 >