python - 在数据框中调用 key 返回 keyerror
问题描述
我正在尝试使用我在使用 concat 函数时分配的键来检索数据,但是它总是返回 keyerror。
例如:
lists = pd.DataFrame({ "no_1": [1,2,3,4]})
x = pd.DataFrame( {"no_2": [3,3,4,5]})
total = pd.concat([lists, x], axis=0, keys=["a", "b"])
print(total["a"])
这将返回 keyerror 'a'。它应该打印在键“a”下分配的数据。
解决方案
调用total["a"]
尝试"a"
从 dataframe中检索列total
,而“a”是此处的索引(行标识符)。
要选择这样的索引,您需要改为使用total.loc["a"]
。
推荐阅读
- machine-learning - 了解应用 MinMaxScaler 后的百分比变化效果?
- arrays - 包含特定索引“i”的子数组的计数是多少?
- python - 如何使用 pywin32 在 Python 中获取特定应用程序窗口的句柄?
- asp.net - 如何在 ASP.NET 身份框架中从用户中删除所有角色
- javascript - Rxjs v6.x 上的类型“事件”.ts(2339) 上不存在属性“clientX”
- angular - 为什么在 empForm.reset(); 之后 标记为无效错误的必填字段?
- node.js - Asnyc Express Midleware 工作的原因是什么?
- visual-studio-code - 在 Visual Studio Code 的 preLaunchTask 中使用 launch.json 中的“输入”选择
- javascript - 如何在 JavaScript 中删除类对象的引用?
- python - 如何根据 Pandas Dataframe 中的列值为绘图上的特定数据点着色