Как удалить строку по индексу python

Аватар пользователя Игорь Гахов
Игорь Гахов
17 октября 2022

Исходный DataFrame:

     Name        LastName     Profession               Gender

0    Charles     Smith        CTO                      male
1    Diane       Walton       Senior JS Dev            female
2    Marjorie    Scott        Project Manager          female
3    Elizabeth   Miller       Junior NodeJS Dev        female
4    Clifton     Bush         DevOps Engeneer          male
-    -           -            -
126  John        Salazar      Middle Java Dev          male
127  Melissa     Jordan       Junior JS Dev            female
128  Tracy       Rose         Middle Layout Designer   female
129  James       Boyd         Senior Python Dev        male
130  Edward      Reid         Middle C/C++ Dev         male

Удаляем строку/строки по значениям индекса c помощью Pandas:

import pandas

# способ 1:

df = df.drop(index=[128])

# способ 2:

df = df.query("index not in [128]")
# или:
df = df.query("index != 128")

Результат:

     Name        LastName     Profession               Gender

0    Charles     Smith        CTO                      male
1    Diane       Walton       Senior JS Dev            female
2    Marjorie    Scott        Project Manager          female
3    Elizabeth   Miller       Junior NodeJS Dev        female
4    Clifton     Bush         DevOps Engeneer          male
-    -           -            -
126  John        Salazar      Middle Java Dev          male
127  Melissa     Jordan       Junior JS Dev            female
129  James       Boyd         Senior Python Dev        male
130  Edward      Reid         Middle C/C++ Dev         male
0 0
Познакомьтесь с основами Python бесплатно