[英]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:假设我有一个数据框 (
DF
) 并且还有一个这样的数组:
rm_indexes = np.array([1, 2, 3, 4, 34, 100, 154, 155, 199])
I want to remove row numbers in rm_indexes
from DF
.我想从
DF
中删除rm_indexes
中的行号。 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). rm_indexes
中的一个表示第一行( DF
的第二行),三个表示数据帧的第三行等(第一行为 0)。 The index column of this data-frame is timestamp.该数据框的索引列是时间戳。
PS.附言。 I have many identical timestamps as the index of data-frame.
我有许多相同的时间戳作为数据帧的索引。
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编辑,在 OP 提供的进一步规范之后:具有相同索引的多行
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
You can simply drop by index.您可以简单地按索引删除。 This will remove entries in df via index 1, 2, 3, 4..etc.. 199.
这将通过索引 1、2、3、4..等删除 df 中的条目。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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.