【Python数据清洗】Index.get_indexer 方法的含义

此方法一般用于Python建模的虚拟变量或者其他数据清洗中

使用方法:

pd.Index(df1).get_indexer(df2)
#表示获取df1中df2内容的索引位置

同理有index就有columns

pd.Columns(df1).get_indexer(df2)

例子
在这里插入图片描述
表示,to_match 中的字符,在 unoque_vals 中的位置索引

喜欢可以关注【小猪课堂】公众号了解更多编程小技巧

KeyError Traceback (most recent call last) File D:\conda\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: '城市' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Cell In[22], line 2 1 # 设置图形大小 ----> 2 plt.plot(data['城市'],data['常住人口'],maeker='o',label='总人口(万人)',linestyle='-') 3 # 绘制农业总产值折线图 4 plt.plot(data['城市'], data['农、林、牧、渔业总产值'], marker='s', label='农业总产值(亿元)', linestyle='--') File D:\conda\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:\conda\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: '城市' [ ]:
03-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值