pandas Simple manipulation of DataFrames Delete / drop rows from DataFrame


Example

let's generate a DataFrame first:

df = pd.DataFrame(np.arange(10).reshape(5,2), columns=list('ab'))

print(df) 
# Output:
#    a  b
# 0  0  1
# 1  2  3
# 2  4  5
# 3  6  7
# 4  8  9

drop rows with indexes: 0 and 4 using drop([...], inplace=True) method:

df.drop([0,4], inplace=True)

print(df)
# Output
#    a  b
# 1  2  3
# 2  4  5
# 3  6  7

drop rows with indexes: 0 and 4 using df = drop([...]) method:

df = pd.DataFrame(np.arange(10).reshape(5,2), columns=list('ab'))

df = df.drop([0,4])

print(df)
# Output:
#    a  b
# 1  2  3
# 2  4  5
# 3  6  7

using negative selection method:

df = pd.DataFrame(np.arange(10).reshape(5,2), columns=list('ab'))

df = df[~df.index.isin([0,4])]

print(df)
# Output:
#    a  b
# 1  2  3
# 2  4  5
# 3  6  7