Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove rows of a dataframe based on the row number

Suppose that I have a data-frame (DF) and also I have an array like this:

rm_indexes = np.array([1, 2, 3, 4, 34, 100, 154, 155, 199])

I want to remove row numbers in rm_indexes from DF. One in rm_indexes means row number one (second row of DF), three means third row of data-frame, etc. (the first row is 0). The index column of this data-frame is timestamp.

PS. I have many identical timestamps as the index of data-frame.

like image 936
Eghbal Avatar asked Apr 25 '19 14:04

Eghbal


2 Answers

Try:

df.drop(df.index[rm_indexes])

example:

import pandas as pd

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]})

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

output

    A   B   C
1   1   1   1
3   3   3   3
5   5   5   5
6   6   6   6
7   7   7   7
8   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.index

pos = [1]
df.reset_index(drop=True, inplace=True)
df.drop(df.index[pos], inplace=True)
df.set_index('idx', inplace=True)

output

    A   B   C
idx         
a   0   0   0
b   2   2   2
a   3   3   3
b   4   4   4
c   5   5   5
c   6   6   6
d   7   7   7
e   8   8   8
like image 57
sentence Avatar answered Sep 27 '22 17:09

sentence


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-1
df.drop([1, 2, 3, 4, 34, 100, 154, 155, 199])  # will drop the rows
df.index = df['myTimeStamp']  # this will restore the index back to timestamp
like image 31
jose_bacoy Avatar answered Sep 27 '22 17:09

jose_bacoy