简体   繁体   English

删除全为零的字符串-Pandas Python

[英]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: 您可以使用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 您可以使用str.contains

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

You get 你得到

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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