【问题标题】:Index rows in Pandas Dataframe not in List of Indexes (Python) [duplicate]Pandas Dataframe 中的索引行不在索引列表中(Python)[重复]
【发布时间】:2021-11-11 14:17:33
【问题描述】:

我正在尝试用列表中不包含的行来索引 Dataframe 的行。例如,这里我们提取数据帧(数据)的第 1,2 和 4 行。但我想提取除这些行之外的所有内容(例如第 3、5 和 6 行):

idx = [1,3,4]
subset= data.iloc[idx , :]

到目前为止,我尝试了这个,但得到以下错误:

try = data.iloc[not idx, :]
ValueError: Location based indexing can only have [integer, integer slice (START point is INCLUDED, END point is EXCLUDED), listlike of integers, boolean array] types

【问题讨论】:

    标签: python pandas list numpy indexing


    【解决方案1】:

    试试这个:

    data.loc[~ data.index.isin(idx)]
    

    【讨论】:

      猜你喜欢
      • 2018-03-19
      • 1970-01-01
      • 2021-06-25
      • 2014-03-30
      • 2016-02-17
      • 2020-09-13
      • 1970-01-01
      • 2014-07-29
      • 2012-10-09
      相关资源
      最近更新 更多