python - 如何从 Pandas 数据框中删除行列表?

我有一个数据框 df :

>>> df
                  sales  discount  net_sales    cogs
STK_ID RPT_Date                                     
600141 20060331   2.709       NaN      2.709   2.245
       20060630   6.590       NaN      6.590   5.291
       20060930  10.103       NaN     10.103   7.981
       20061231  15.915       NaN     15.915  12.686
       20070331   3.196       NaN      3.196   2.710
       20070630   7.907       NaN      7.907   6.459

然后我想删除列表中指示的具有某些序列号的行,假设这里是 [1,2,4], 然后离开:

                  sales  discount  net_sales    cogs
STK_ID RPT_Date                                     
600141 20060331   2.709       NaN      2.709   2.245
       20061231  15.915       NaN     15.915  12.686
       20070630   7.907       NaN      7.907   6.459

如何或什么功能可以做到这一点?

最佳答案

使用 DataFrame.drop并传递给它一系列索引标签:

In [65]: df
Out[65]: 
       one  two
one      1    4
two      2    3
three    3    2
four     4    1


In [66]: df.drop(df.index[[1,3]])
Out[66]: 
       one  two
one      1    4
three    3    2

https://stackoverflow.com/questions/14661701/

相关文章:

linux - 杀死分离的 screen session

linux - 在 Bash 脚本中通过管道传入/传出剪贴板

python - 在 Python 3 中 generator.next() 是可见的吗?

python - 下标序列时Python中的::(双冒号)是什么?

python - 如何正确确定当前脚本目录?

linux - 如何在 Linux 上使用 grep 仅显示文件名?

python - Pandas 'count(distinct)' 等效

linux - 定义带或不带导出的变量

python - 如何反转 x 或 y 轴

linux - 如何将输出重定向到文件和标准输出