【发布时间】:2021-08-26 16:30:14
【问题描述】:
这应该很简单,但让我感到困惑:
import numpy as np
import pandas as pd
df = pd.DataFrame([[1,2,3],[1,4,5],[7,12,3]],columns=["A","B","C"])
subdf = df[df['A'] == 1]
print(subdf)
如预期:
subdf
A B C
0 1 2 3
1 1 4 5
但是
oneline = subdf.loc[subdf['C'] == 5]
print ("One Line:")
print(oneline['B'])
让我:
1 4
Name: B, dtype: int64
我尝试了几种方法来提取“4”并不断收到关键错误:
print(oneline['B'].at[0])
Traceback (most recent call last):
文件“/usr/local/lib/python3.6/dist-packages/pandas/core/indexes/base.py”,第 2898 行,在 get_loc 返回 self._engine.get_loc(casted_key) 文件“pandas/_libs/index.pyx”,第 70 行,在 pandas._libs.index.IndexEngine.get_loc 文件“pandas/_libs/index.pyx”,第 101 行,在 pandas._libs.index.IndexEngine.get_loc 文件“pandas/_libs/hashtable_class_helper.pxi”,第 1032 行,在 pandas._libs.hashtable.Int64HashTable.get_item 文件“pandas/_libs/hashtable_class_helper.pxi”,第 1039 行,在 pandas._libs.hashtable.Int64HashTable.get_item 关键错误:0
尝试挖掘 .loc() 文档,但我没有找到答案。有什么想法吗?
【问题讨论】:
-
oneline['B'].values[0]行应该按照评论中的建议工作。我们正在做oneline['B'].values因为oneline['B']是pands.Series类型。
标签: python