Remove rows of a dataframe based on the row number Remove rows of a dataframe based on the row number pandas pandas

Remove rows of a dataframe based on the row number


Try:

df.drop(df.index[rm_indexes])

example:

import pandas as pddf = pd.DataFrame({"A":[0,1,2,3,4,5,6,7,8],                   "B":[0,1,2,3,4,5,6,7,8],                   "C":[0,1,2,3,4,5,6,7,8]})pos = [0,2,4]df.drop(df.index[pos], inplace=True)

output

    A   B   C1   1   1   13   3   3   35   5   5   56   6   6   67   7   7   78   8   8   8

EDIT, after further specification provided by OP: multiple rows with the same index

df = pd.DataFrame({"A":[0,1,2,3,4,5,6,7,8],                   "B":[0,1,2,3,4,5,6,7,8],                   "C":[0,1,2,3,4,5,6,7,8],},                   index=["a","b","b","a","b","c","c","d","e"])df['idx'] = df.indexpos = [1]df.reset_index(drop=True, inplace=True)df.drop(df.index[pos], inplace=True)df.set_index('idx', inplace=True)

output

    A   B   Cidx         a   0   0   0b   2   2   2a   3   3   3b   4   4   4c   5   5   5c   6   6   6d   7   7   7e   8   8   8


You can simply drop by index. This will remove entries in df via index 1, 2, 3, 4..etc.. 199.

df.reset_index()    #this will change the index from timestamp to 0,1,2...n-1df.drop([1, 2, 3, 4, 34, 100, 154, 155, 199])  # will drop the rowsdf.index = df['myTimeStamp']  # this will restore the index back to timestamp