简体   繁体   中英

Dropping Dataframe rows based on name

I have the following dataframe df where I am trying to drop all rows having curv_typ as PYC_RT or 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

My code to do this is as follows. However, for some reason df turns out to be exactly the same as above after running the code below:

df = pd.DataFrame.from_csv("ECB.tsv", sep="\t", index_col=False)
df[df["curv_typ"] != "PYC_RT"]
df[df["curv_typ"] != "YCIF_RT"]

You need to assign the resulting DataFrame to the original DataFrame (thus, over-writing it):

df = df[df["curv_typ"] != "PYC_RT"]
df = df[df["curv_typ"] != "YCIF_RT"]

Use isin and negate ~ the boolean condition for the mask:

In [76]:
df[~df['curv_typ'].isin(['PYC_RT', 'YCIF_RT'])]

Out[76]:
Empty DataFrame
Columns: [curv_typ, maturity, bonds, 2015M06D19, 2015M06D18, 2015M06D17]
Index: []

Note that this returns nothing on your sample dataset

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM