python教程:df.index.get_indexer() 的示例用法

df.index.get_indexer(target) 用于获取 target 中每个元素在 DataFrame 索引中的位置。如果元素不在索引中,返回 -1。

# -*- coding: utf-8 -*-
# @Author : 小红牛
# 微信公众号:WdPython
import pandas as pd

# 创建示例 DataFrame
data = {
    "A": [10, 20, 30, 40],
    "B": ["x", "y", "z", "w"]
}
index = pd.to_datetime(["2023-01-01", "2023-01-02", "2023-01-04", "2023-01-05"])
df = pd.DataFrame(data, index=index)

print("原始 DataFrame:")
print(df)

# 目标索引(包含存在的和不存在的日期)
target_dates = [
    "2023-01-01",  # 存在
    "2023-01-03",  # 不存在
    "2023-01-05"   # 存在
]

# 使用 get_indexer 查找位置
positions = df.index.get_indexer(target_dates)

print("\n目标日期在原始索引中的位置:")
print(positions)

输出内容

原始 DataFrame:
                A  B
2023-01-01  10  x
2023-01-02  20  y
2023-01-04  30  z
2023-01-05  40  w

目标日期在原始索引中的位置:
[ 0 -1  3]

0: “2023-01-01” 是索引的第 0 个位置。
-1: “2023-01-03” 不在索引中。
3: “2023-01-05” 是索引的第 3 个位置。

通过 method 参数指定如何处理缺失值:

# 找到最近的索引位置(向前填充)
positions_nearest = df.index.get_indexer(
    target_dates,
    method="nearest"
)
print("\n使用 method='nearest' 的位置:")
print(positions_nearest)

输出

使用 method='nearest' 的位置:
[0 2 3]

“2023-01-03” 的最近索引是 “2023-01-02”(索引 1)或 “2023-01-04”(索引 2),取决于具体实现。

完毕!!感谢您的收看

----------★★跳转到历史博文集合★★----------

我的零基础Python教程,Python入门篇 进阶篇 视频教程 Py安装py项目 Python模块 Python爬虫 Json Xpath 正则表达式 Selenium Etree CssGui程序开发 Tkinter Pyqt5 列表元组字典数据可视化 matplotlib 词云图 Pyecharts 海龟画图 Pandas Bug处理 电脑小知识office自动化办公 编程工具 NumPy Pygame

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
from sklearn.feature_extraction.text import TfidfVectorizer vectorizer = TfidfVectorizer() X = vectorizer.fit_transform(data['text_column'])报错显示: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: 'text_column' The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Cell In[10], line 4 1 from sklearn.feature_extraction.text import TfidfVectorizer 3 vectorizer = TfidfVectorizer() ----> 4 X = vectorizer.fit_transform(data['text_column']) 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: 'text_column'怎么解决呢
最新发布
07-14
KeyError Traceback (most recent call last) File D:\anaconda\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[62], line 2 1 # 按月统计故障报警次数 ----> 2 alarm_counts = df[df['故障指示灯状态'] == 1].groupby('月份').size().reset_index(name='故障报警次数') File D:\anaconda\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:\anaconda\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-14
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:163, in pandas._libs.index.IndexEngine.get_loc() File pandas\_libs\hashtable_class_helper.pxi:2131, in pandas._libs.hashtable.Int64HashTable.get_item() File pandas\_libs\hashtable_class_helper.pxi:2140, in pandas._libs.hashtable.Int64HashTable.get_item() KeyError: 1 The above exception was the direct cause of the following exception: KeyError Traceback (most recent call last) Input In [2], in <cell line: 2>() 1 import matplotlib.pyplot as plt ----> 2 plt.plot(data[0], data[1]) 3 plt.xlabel('X轴') 4 plt.ylabel('Y轴') 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: 1
03-27
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值