简体   繁体   English

为什么在 Python Pandas dtype float64 上出现错误?

[英]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.

相关问题 我收到此错误消息:无法根据规则“安全”将数组数据从 dtype('O') 转换为 dtype('float64') - I get this error message: Cannot cast array data from dtype('O') to dtype('float64') according to the rule 'safe' pandas 未将 object dtype 转换为 float64,即使在 df.astype('float64') 无错误执行后也是如此 - pandas not converting an object dtype to float64 even after error free execution of df.astype('float64') Python Pandas read_csv dtype 无法将“字符串”转换为“float64” - Python Pandas read_csv dtype fails to covert "string" to "float64" 如何从 Pandas 查询中删除 float64 - How do I remove float64 from Pandas query 为什么numpy.dtype(&#39;float64&#39;)特别? - Why is numpy.dtype('float64') special? 如何在我的 numpy 数组中找到 NaN/无穷大/对于 dtype(&#39;float64&#39;) 来说太大的值? - How do I find the values in my numpy array that are NaN/infinity/too large for dtype('float64')? Python 错误帮助:“ValueError:输入包含 NaN、无穷大或对于 dtype('float64') 而言太大的值。” - Python error help: “ValueError: Input contains NaN, infinity or a value too large for dtype('float64').” Python 3:错误:ValueError:输入包含 NaN、无穷大或对于 dtype('float64')来说太大的值 - Python 3: Error: ValueError: Input contains NaN, infinity or a value too large for dtype('float64') 如何使用 python 将列中的 dtype 从 object 更改为 float64? - How can i change dtype from object to float64 in a column, using python? 错误说我有 NaN、无穷大或对于 dtype('float64') 来说太大的值 - Error says I have NaN, infinity or a value too large for dtype('float64')
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM