使用df.loc在pandas中按行选择

时间:2018-04-24 02:45:08

标签: pandas dataframe jupyter-notebook

使用jupyter笔记本我将一个excel文件作为pandas数据框导入,命名为“clean”。当尝试按行名称选择数据时,clean.loc('Method Blank')会抛出一个错误,指出'Method Blank'不在索引中。我相信它可能与具有空值的数据库的索引有关,其中存在后续数据。有什么建议吗?

clean.shape

(8, 2)

type(clean)

pandas.core.frame.DataFrame

clean

        Phenol Batch QC Check   Unnamed: 1
NaN     NaN     PNL Results     Acceptance
QC Parameter    (ug/L)  Criteria
Method Blank    1.1284  <2 ug/l
Reporting Limit (RL) Verification Standard (2.0 ug/L)   2.5318  ±40%
Reporting Limit (RL) Verification Standard (4.0 ug/L)   4.0967  ±40%
Laboratory Control Sample (LCS)     20.2061     ±20%
Quality Control Sample (QCS)    19.9668     ±20%
Calibration Verification (20.0 ug/L)    20.1203     ±20%

clean.loc['Method Blank']

---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
~/anaconda3/lib/python3.6/site-packages/pandas/core/indexing.py in _has_valid_type(self, key, axis)
   1505                 if not ax.contains(key):
-> 1506                     error()
   1507             except TypeError as e:

1 个答案:

答案 0 :(得分:0)

我不知道这是一个多索引的数据帧。

使用clean.ustack(level = 0).loc ['Method Blank']我能够让它返回相应的数据。