简体   繁体   中英

Drop string that is all zeros - pandas python

I have a dataframe of strings that looks like this:

In [58]: d['upin'].head()
Out[58]:
0    'H8409  
1    'H8409
2    .31961
3    .31961
4    000000
Name: upin, dtype: object

I want to drop the rows that have only zeros, ie the last row in this example. I haven't found a nice regexp way of doing this, and I'm not sure what else to try.

This should work if I understood your problem correctly:

df = pd.DataFrame()
df['pin'] = ["00000","F4923","'222R","0","00001"]
ndf = df[~(df['pin'].str.contains('^0+$'))]

You can use pd.to_numeric:

df.loc[pd.to_numeric(df.pin,errors='coerce').ne(0)]
Out[278]: 
     pin
1  F4923
2  '222R
4  00001

You can use str.contains

df = df[df.upin.str.contains('[^0]')]

You get

    upin
0   'H8409
1   'H8409
2   .31961
3   .31961

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