1

我有一个数据框“moment_f”。我必须从 moment_f 中删除一些包含名称“AH”的行。但是我不会删除每个“AH”行,所以我创建了一个包含我要删除的“AH”的新数据框。

ah1 = moment_f[moment_f["TextGridLabel"] == "AH"]
ah_d = ah1.iloc[::2] 
# got the odd rows of "AH" which is what I need to get rid of from the original dataframe "moment_f". 

所以现在我想从数据帧“moment_f”中删除数据帧“ah_d”。

串联的反面是什么?我试过drop,split等,但它不起作用。

4

2 回答 2

0

您可以通过仅使用索引来避免创建“子集数据框” 。这是可能的,iloc但并非微不足道。重要的是,以下解决方案仅适用于整数位置位置,而不适用于索引标签,即它不假定唯一索引。

import numpy as np

# some example dataframe
df = pd.DataFrame({'A': [1, 2, 3, 1, 1, 2, 1, 1, 1, 3]})

# extract indices to remove
idx = np.where(df['A'] == 1)[0][::2]  # array([0, 4, 7], dtype=int64)

# include all indices which do not match idx
res = df.iloc[~np.in1d(np.arange(df['A'].shape[0]), idx)]

print(res)

   A
1  2
2  3
3  1
5  2
6  1
8  1
9  3

如果您的索引是常规的pd.RangeIndex,即0, 1, ..., n,您可以通过以下方式按标签删除pd.DataFrame.drop

res = df.drop(idx, axis='rows')
于 2018-10-16T08:47:55.703 回答
0

IIUC 需要:

df = moment_f[~moment_f.index.isin(ah_d.index)]
于 2018-10-16T07:46:54.597 回答