pandas.DataFrame.to_dict()的使用详解

博客主要对pandas.DataFrame.to_dict()的使用进行了详细讲解,涉及Python中pandas库的相关操作,有助于使用者更好地掌握该方法的运用。
KeyError Traceback (most recent call last) File /opt/conda/envs/anaconda-2024.02-py310/lib/python3.10/site-packages/pandas/core/indexes/base.py:3791, in Index.get_loc(self, key) 3790 try: -> 3791 return self._engine.get_loc(casted_key) 3792 except KeyError as err: File index.pyx:152, in pandas._libs.index.IndexEngine.get_loc() File index.pyx:181, in pandas._libs.index.IndexEngine.get_loc() File pandas/_libs/hashtable_class_helper.pxi:7080, in pandas._libs.hashtable.PyObjectHashTable.get_item() File pandas/_libs/hashtable_class_helper.pxi:7088, in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'Yr' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Cell In[9], line 23 20 df = df.rename(columns=rename_dict) 22 # 强制转换为数值类型,无法转换的变为 NaN ---> 23 df['Yr'] = pd.to_numeric(df['Yr'], errors='coerce') 24 df['Mn'] = pd.to_numeric(df['Mn'], errors='coerce') 25 df['CO2'] = pd.to_numeric(df['CO2'], errors='coerce') File /opt/conda/envs/anaconda-2024.02-py310/lib/python3.10/site-packages/pandas/core/frame.py:3893, in DataFrame.__getitem__(self, key) 3891 if self.columns.nlevels > 1: 3892 return self._getitem_multilevel(key) -> 3893 indexer = self.columns.get_loc(key) 3894 if is_integer(indexer): 3895 indexer = [indexer] File /opt/conda/envs/anaconda-2024.02-py310/lib/python3.10/site-packages/pandas/core/indexes/base.py:3798, in Index.get_loc(self, key) 3793 if isinstance(casted_key, slice) or ( 3794 isinstance(casted_key, abc.Iterable) 3795 and any(isinstance(x, slice) for x in casted_key) 3796 ): 3797 raise InvalidIndexError(key) -> 3798 raise KeyError(key) from err 3799 except TypeError: 3800 # If we have a listlike key, _check_indexing_error will raise 3801 # InvalidIndexError. Otherwise we fall through and re-raise 3802 # the TypeError. 3803 self._check_indexing_error(key) KeyError: 'Yr'
最新发布
11-19
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值