pandas re_index 和rename

本文对比了Pandas库中DataFrame的reindex与rename方法,详细介绍了这两种方法的功能差异及应用场景。rename用于修改列名,而reindex则用于自定义索引顺序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

今天在处理数据的时候,需要dataframe进行重排,记忆中有两个相似的方法reindex和rename,这里记录一下常见用法和区别:

rename:重命名,就是对col进行命名的修改,他只改变col的名字,相当于起了个别名,原来叫a,以后叫b

reindex:重新索引,他可以修改还列的索引关系以及index‘行的索引关系

rename:

官方文档给的示例:

>>> df = pd.DataFrame({"A": [1, 2, 3], "B": [4, 5, 6]})
>>> df.rename(index=str, columns={"A": "a", "B": "c"})
   a  c
0  1  4
1  2  5
2  3  6
reindex的示例可参见“利用python进行数据分析”p126:

它提供的是对原来索引顺序的自定义排列,传入columns可以reindex列

也可以参阅官方文档:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reindex.html


# 2.2 数据预处理 wujing['购药时间'] = pd.to_datetime(wujing['购药时间']) # 将“购药时间”转换为时间格式 wujing.set_index('购药时间', inplace=True) # 设置时间索引 wujing.sort_index(inplace=True) # 按时间排序 wujing.dropna(inplace=True) # 删除缺失值 KeyError Traceback (most recent call last) File ~\.conda\envs\Wu\lib\site-packages\pandas\core\indexes\base.py:3653, in Index.get_loc(self, key) 3652 try: -> 3653 return self._engine.get_loc(casted_key) 3654 except KeyError as err: File ~\.conda\envs\Wu\lib\site-packages\pandas\_libs\index.pyx:147, in pandas._libs.index.IndexEngine.get_loc() File ~\.conda\envs\Wu\lib\site-packages\pandas\_libs\index.pyx:176, 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: '购药时间' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Cell In[17], line 2 1 # 2.2 数据预处理 ----> 2 wujing['购药时间'] = pd.to_datetime(wujing['购药时间']) # 将“购药时间”转换为时间格式 3 wujing.set_index('购药时间', inplace=True) # 设置时间索引 4 wujing.sort_index(inplace=True) # 按时间排序 File ~\.conda\envs\Wu\lib\site-packages\pandas\core\frame.py:3761, in DataFrame.__getitem__(self, key) 3759 if self.columns.nlevels > 1: 3760 return self._getitem_multilevel(key) -> 3761 indexer = self.columns.get_loc(key) 3762 if is_integer(indexer): 3763 indexer = [indexer] File ~\.conda\envs\Wu\lib\site-packages\pandas\core\indexes\base.py:3655, in Index.get_loc(self, key) 3653 return self._engine.get_loc(casted_key) 3654 except KeyError as err: -> 3655 raise KeyError(key) from err 3656 except TypeError: 3657 # If we have a listlike key, _check_indexing_error will raise 3658 # InvalidIndexError. Otherwise we fall through and re-raise 3659 # the TypeError. 3660 self._check_indexing_error(key) KeyError: '购药时间' 报错
最新发布
08-04
--------------------------------------------------------------------------- KeyError Traceback (most recent call last) File D:\anaconda3\lib\site-packages\pandas\core\indexes\base.py:3802, in Index.get_loc(self, key, method, tolerance) 3801 try: -> 3802 return self._engine.get_loc(casted_key) 3803 except KeyError as err: File D:\anaconda3\lib\site-packages\pandas\_libs\index.pyx:138, in pandas._libs.index.IndexEngine.get_loc() File D:\anaconda3\lib\site-packages\pandas\_libs\index.pyx:165, in pandas._libs.index.IndexEngine.get_loc() File pandas\_libs\hashtable_class_helper.pxi:5745, in pandas._libs.hashtable.PyObjectHashTable.get_item() File pandas\_libs\hashtable_class_helper.pxi:5753, in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'price' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Cell In[5], line 13 11 data = pd.read_csv('USA_Housing.csv') 12 X = data['Avg. Area Income'].values.reshape(-1,1) ---> 13 y = data['price'].values.reshape(-1,1) 15 # 标准化处理 16 scaler_X = StandardScaler() File D:\anaconda3\lib\site-packages\pandas\core\frame.py:3807, in DataFrame.__getitem__(self, key) 3805 if self.columns.nlevels > 1: 3806 return self._getitem_multilevel(key) -> 3807 indexer = self.columns.get_loc(key) 3808 if is_integer(indexer): 3809 indexer = [indexer] File D:\anaconda3\lib\site-packages\pandas\core\indexes\base.py:3804, in Index.get_loc(self, key, method, tolerance) 3802 return self._engine.get_loc(casted_key) 3803 except KeyError as err: -> 3804 raise KeyError(key) from err 3805 except TypeError: 3806 # If we have a listlike key, _check_indexing_error will raise 3807 # InvalidIndexError. Otherwise we fall through and re-raise 3808 # the TypeError. 3809 self._check_indexing_error(key) KeyError: 'price'
03-18
KeyError Traceback (most recent call last) File E:\anaconda\lib\site-packages\pandas\core\indexes\base.py:3621, in Index.get_loc(self, key, method, tolerance) 3620 try: -> 3621 return self._engine.get_loc(casted_key) 3622 except KeyError as err: File E:\anaconda\lib\site-packages\pandas\_libs\index.pyx:136, in pandas._libs.index.IndexEngine.get_loc() File E:\anaconda\lib\site-packages\pandas\_libs\index.pyx:144, in pandas._libs.index.IndexEngine.get_loc() File pandas\_libs\index_class_helper.pxi:41, in pandas._libs.index.Int64Engine._check_type() KeyError: 'X' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Input In [3], in <cell line: 5>() 3 # 折线图示例 4 plt.figure(figsize=(10, 6)) ----> 5 plt.plot(data['X'], data['Y'], marker='o', linestyle='-', color='blue') 6 plt.title('X与Y的关系') 7 plt.xlabel('X轴') File E:\anaconda\lib\site-packages\pandas\core\frame.py:3505, in DataFrame.__getitem__(self, key) 3503 if self.columns.nlevels > 1: 3504 return self._getitem_multilevel(key) -> 3505 indexer = self.columns.get_loc(key) 3506 if is_integer(indexer): 3507 indexer = [indexer] File E:\anaconda\lib\site-packages\pandas\core\indexes\base.py:3623, in Index.get_loc(self, key, method, tolerance) 3621 return self._engine.get_loc(casted_key) 3622 except KeyError as err: -> 3623 raise KeyError(key) from err 3624 except TypeError: 3625 # If we have a listlike key, _check_indexing_error will raise 3626 # InvalidIndexError. Otherwise we fall through and re-raise 3627 # the TypeError. 3628 self._check_indexing_error(key) KeyError: 'X' <Figure size 720x432 with 0 Axes>
03-27
输入# 3. 文本清洗函数这部分代码后,报错:-------------------------------------------------------------------------- KeyError Traceback (most recent call last) File /opt/anaconda3/envs/deep/lib/python3.12/site-packages/pandas/core/indexes/base.py:3805, in Index.get_loc(self, key) 3804 try: -> 3805 return self._engine.get_loc(casted_key) 3806 except KeyError as err: File index.pyx:167, in pandas._libs.index.IndexEngine.get_loc() File index.pyx:196, in pandas._libs.index.IndexEngine.get_loc() File pandas/_libs/hashtable_class_helper.pxi:7081, in pandas._libs.hashtable.PyObjectHashTable.get_item() File pandas/_libs/hashtable_class_helper.pxi:7089, in pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: '博文内容' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Cell In[5], line 1 ----> 1 df['清洗文本'] = df['博文内容'].apply(clean_text) File /opt/anaconda3/envs/deep/lib/python3.12/site-packages/pandas/core/frame.py:4102, in DataFrame.__getitem__(self, key) 4100 if self.columns.nlevels > 1: 4101 return self._getitem_multilevel(key) -> 4102 indexer = self.columns.get_loc(key) 4103 if is_integer(indexer): 4104 indexer = [indexer] File /opt/anaconda3/envs/deep/lib/python3.12/site-packages/pandas/core/indexes/base.py:3812, in Index.get_loc(self, key) 3807 if isinstance(casted_key, slice) or ( 3808 isinstance(casted_key, abc.Iterable) 3809 and any(isinstance(x, slice) for x in casted_key) 3810 ): 3811 raise InvalidIndexError(key) -> 3812 raise KeyError(key) from err 3813 except TypeError: 3814 # If we have a listlike key, _check_indexing_error will raise 3815 # InvalidIndexError. Otherwise we fall through and re-raise 3816 # the TypeError. 3817 self._check_indexing_error(key) KeyError: '博文内容',是为什么呢
07-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值