python - 使用 .loc() 时的 Pandas KeyError
问题描述
我有一个熊猫数据框portfolio
,其键是日期。我正在尝试通过访问多行
print(portfolio.loc[['2007-02-26','2008-02-06'],:])
,
但我收到一个错误
KeyError: "None of [Index(['2007-02-26', '2008-02-06'], dtype='object', name='Date')] are in the [index]"
但是,print(portfolio.loc['2007-02-26',:])
成功返回
holdings 1094.6124
pos_diff 100.0000
cash 98905.3876
total 100000.0000
returns 0.0000
Name: 2007-02-26 00:00:00, dtype: float64
这不是有效的格式-->df.loc[['key1', 'key2', 'key3'], 'Column1]
吗?
解决方案
似乎问题在于从字符串到时间戳的类型转换。因此,解决方案是将标签集显式转换为 DateTime,然后再将它们传递给loc
:
df = pd.DataFrame({"a" : range(5)}, index = pd.date_range("2020-01-01", freq="1D", periods=5))
print(df)
==>
a
2020-01-01 0
2020-01-02 1
2020-01-03 2
2020-01-04 3
2020-01-05 4
try:
df.loc[["2020-01-01", "2020-01-02"], :]
except Exception as e:
print (e)
==>
"None of [Index(['2020-01-01', '2020-01-02'], dtype='object')] are in the [index]"
# But - if you convert the labels to datetime before calling loc,
# it works fine.
df.loc[pd.to_datetime(["2020-01-01", "2020-01-02"]), :]
===>
a
2020-01-01 0
2020-01-02 1
推荐阅读
- javascript - 不能在嵌套的 TouchableOpacity 中工作
- python - Selenium for 循环只执行一次
- excel - 使用 EXCEL 计算依赖于行中其他单元格的单元格 - 创建矩阵
- git - 如何忽略文件夹中不以@开头的所有目录?
- mongodb - 运行使用 python 编写为字符串的 mongo 查询,而不使用任何库函数(find_one 等)
- aws-lambda - 错误:“无服务器离线:找不到路由。” 尝试使用无服务器离线运行 Angular 9 Universal 时
- sql - 如何删除 PosgreSQL 中的镜像结果?
- python - 熊猫替换不替换整个字符串
- c++ - 无法使用几何着色器
- php - Ajax 调用错误不允许我查看表单中缺少的详细信息