我有以下数据框df
,我试图删除curv_typ
为PYC_RT
或YCIF_RT
的所有行。
curv_typ maturity bonds 2015M06D19 2015M06D18 2015M06D17 \
0 PYC_RT Y1 GBAAA -0.24 -0.25 -0.23
1 PYC_RT Y1 GBA_AAA -0.05 -0.05 -0.05
2 PYC_RT Y10 GBAAA 0.89 0.92 0.94
我的代码如下。但是,由于某种原因df
在运行以下代码后与上述完全相同:
df = pd.DataFrame.from_csv("ECB.tsv", sep="\t", index_col=False)
df[df["curv_typ"] != "PYC_RT"]
df[df["curv_typ"] != "YCIF_RT"]
答案 0 :(得分:1)
您需要将结果DataFrame
分配给原始DataFrame
(因此,覆盖它):
df = df[df["curv_typ"] != "PYC_RT"]
df = df[df["curv_typ"] != "YCIF_RT"]
答案 1 :(得分:1)
使用isin
并否定~
掩码的布尔条件:
In [76]:
df[~df['curv_typ'].isin(['PYC_RT', 'YCIF_RT'])]
Out[76]:
Empty DataFrame
Columns: [curv_typ, maturity, bonds, 2015M06D19, 2015M06D18, 2015M06D17]
Index: []
请注意,这不会在您的样本数据集中返回任何内容