[英]Why do I get an error on a Python Pandas dtype float64?
I'm in a jupyter notebook on a Windows PC.我在 Windows PC 上的 jupyter 笔记本中。 I've read in a dataframe I'm calling tran as follows我读了一个数据帧,我调用 tran 如下
tran = pd.read_csv("https://raw.githubusercontent.com/m1ngle/TRCount/main/TRCountUS.csv")
When I look at data types for the entire dataframe, it works great当我查看整个数据框的数据类型时,效果很好
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
When I attempt to call, or work with any of the float64 columns I get an error当我尝试调用或使用任何 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'
This error does not occur when I work with any of the int64 dtypes.当我使用任何 int64 dtypes 时,不会发生此错误。
tran['YMTF'].dtype
dtype('int64')
Can anyone help me out?谁能帮我吗?
While reading the .csv
file, pandas also read in the whitespace that came along with the columns.在读取.csv
文件时,pandas 还会读取与列一起出现的空格。 When I did tran[' MTFPer '].dtype
instead of tran['MTFPer'].dtype
, pandas gave me the correct answer.当我使用tran[' MTFPer '].dtype
而不是tran['MTFPer'].dtype
,pandas 给了我正确的答案。
Maybe clean up the data itself a little bit, or you could clean up the column names like so:也许清理一下数据本身,或者你可以清理列名,如下所示:
tran.columns = [c.strip() for c in tran.columns]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.