Python 将数据从字符串转换为int时出错
我有一个数据框Python 将数据从字符串转换为int时出错,python,pandas,dataframe,type-conversion,Python,Pandas,Dataframe,Type Conversion,我有一个数据框已清理\u bp['VISITCODE'],看起来像: 0 1 1 2 2 3 3 6 4 9 5 12 6 15 其中非索引列由字符串组成。 我想通过执行以下操作将它们转换为整数: for i in range(len(cleaned_bp['VISITCODE'])): cleaned_bp['VISITCODE'][i] = int(cleane
已清理\u bp['VISITCODE']
,看起来像:
0 1
1 2
2 3
3 6
4 9
5 12
6 15
其中非索引列由字符串组成。
我想通过执行以下操作将它们转换为整数:
for i in range(len(cleaned_bp['VISITCODE'])):
cleaned_bp['VISITCODE'][i] = int(cleaned_bp['VISITCODE'][i])
但我得到了这个错误:
---------------------------------------------------------------------------
KeyError Traceback (most recent call last)
<ipython-input-42-4d6508c1abda> in <module>()
1 for i in range(len(cleaned_bp['VISITCODE'])):
----> 2 cleaned_bp['VISITCODE'][i] = int(cleaned_bp['VISITCODE'][i])
~/anaconda3/lib/python3.6/site-packages/pandas/core/series.py in __getitem__(self, key)
599 key = com._apply_if_callable(key, self)
600 try:
--> 601 result = self.index.get_value(self, key)
602
603 if not is_scalar(result):
~/anaconda3/lib/python3.6/site-packages/pandas/core/indexes/base.py in get_value(self, series, key)
2475 try:
2476 return self._engine.get_value(s, k,
-> 2477 tz=getattr(series.dtype, 'tz', None))
2478 except KeyError as e1:
2479 if len(self) > 0 and self.inferred_type in ['integer', 'boolean']:
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item()
KeyError: 13
---------------------------------------------------------------------------
KeyError回溯(最近一次呼叫最后一次)
在()
1表示范围内的i(len(清洁的_bp['VISITCODE']):
---->2已清理的_-bp['VISITCODE'][i]=int(已清理的_-bp['VISITCODE'][i])
~/anaconda3/lib/python3.6/site-packages/pandas/core/series.py in\uuuuu getitem\uuuuu(self,key)
599 key=com.\u如果可调用(key,self),则应用
600次尝试:
-->601结果=self.index.get_值(self,key)
602
603如果不是标量(结果):
get_值中的~/anaconda3/lib/python3.6/site-packages/pandas/core/index/base.py(self、series、key)
2475尝试:
2476返回自引擎。获取值(s,k,
->2477 tz=getattr(series.dtype,“tz”,无))
2478除键错误为e1外:
2479如果len(self)>0且self.u输入['integer','boolean']:
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_value()
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc()
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.Int64HashTable.get_item()中
pandas/_libs/hashtable_class_helper.pxi在pandas._libs.hashtable.Int64HashTable.get_item()中
关键错误:13
我怎么会在这里 试试看:
for i in range(len(cleaned_bp['VISITCODE'])):
cleaned_bp['VISITCODE'].iloc[i] = int(cleaned_bp['VISITCODE'].iloc[i])
这将使用索引中的位置,而不是索引本身。尝试:
for i in range(len(cleaned_bp['VISITCODE'])):
cleaned_bp['VISITCODE'].iloc[i] = int(cleaned_bp['VISITCODE'].iloc[i])
这将使用索引中的位置,而不是索引本身。如果使用熊猫,您可以尝试:
cleaned_bp.VISITCODE.astype(int)
如果您正在使用熊猫,您可以尝试:
cleaned_bp.VISITCODE.astype(int)
有没有理由不直接使用
cleaned\u bp['VISITCODE'].astype(int)
或pandas.to\u numeric
。我将使用它们。仍然很好奇为什么问题中的方法不起作用,因为cleaned\u np['VISITCODE'][i]
将尝试访问与列相关的索引,而不是像列表一样访问位置。这意味着您的索引列与范围(len(your_df))
不同。。。如果您将其更改为iloc[i]
。。。您可能需要阅读索引。是否有理由不使用cleaned\u bp['VISITCODE'].astype(int)
或pandas.to\u numeric
?@JonClements不知道它们。我将使用它们。仍然很好奇为什么问题中的方法不起作用,因为cleaned\u np['VISITCODE'][i]
将尝试访问与列相关的索引,而不是像列表一样访问位置。这意味着您的索引列与范围(len(your_df))
不同。。。如果您将其更改为iloc[i]
。。。您可能需要阅读有关索引的内容。