pandas re_index 和rename

今天在处理数据的时候,需要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


KeyError Traceback (most recent call last) File D:\python\Lib\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: 'date' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Cell In[65], line 6 3 df = pd.read_csv(r"D:\dashuju\zuoye\微博_时间.csv", sep=",",encoding="ansi") # 同时指定分隔符 4 print(df.head()) ----> 6 df['date'] = pd.to_datetime(df['date']) 8 # 设置目标年月 9 year, month = 2023, 8 File D:\python\Lib\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 D:\python\Lib\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: 'date' Click to add a cell.
03-12
Traceback (most recent call last): File "C:\Users\liuguiqin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\indexes\base.py", line 3805, in get_loc return self._engine.get_loc(casted_key) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "index.pyx", line 167, in pandas._libs.index.IndexEngine.get_loc File "index.pyx", line 196, in pandas._libs.index.IndexEngine.get_loc File "pandas\\_libs\\hashtable_class_helper.pxi", line 7081, in pandas._libs.hashtable.PyObjectHashTable.get_item File "pandas\\_libs\\hashtable_class_helper.pxi", line 7089, in pandas._libs.hashtable.PyObjectHashTable.get_item KeyError: '金额' The above exception was the direct cause of the following exception: Traceback (most recent call last): File "C:\Users\liuguiqin\PycharmProjects\pythonProject\1.py", line 192, in <module> main() File "C:\Users\liuguiqin\PycharmProjects\pythonProject\1.py", line 166, in main feature_df = create_features(raw_df) ^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\liuguiqin\PycharmProjects\pythonProject\1.py", line 53, in create_features df['amount'] = df['金额'].str.replace('[¥¥,]', '', regex=True).astype(float) ~~^^^^^^^^ File "C:\Users\liuguiqin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\frame.py", line 4102, in __getitem__ indexer = self.columns.get_loc(key) ^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\liuguiqin\AppData\Local\Programs\Python\Python312\Lib\site-packages\pandas\core\indexes\base.py", line 3812, in get_loc raise KeyError(key) from err KeyError: '金额' Process finished with exit code 1
03-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值