从pandas数据框中删除字符串时出错 - Python

时间:2014-10-10 14:33:59

标签: python pandas

我有一个set和一个函数来删除我的pandas数据框的变量'nstandar'中的字符串。集合,函数和pandas数据框如下:

setc={'adr','company','corporation','energy','etf','group','holdings','inc','international','ltd'}

def quitarc(x):
    x=''.join(a for a in x if a not in setc)
    return x

    Company name                         nstandar   
0   1-800-FLOWERS.COM                    1800flowerscom
1   1347 PROPERTY INS HLDGS INC          1347 property ins hldgs inc    
2   1ST CAPITAL BANK                     1st capital bank   
3   1ST CENTURY BANCSHARES INC           1st century bancshares inc 
4   1ST CONSTITUTION BANCORP             1st constitution bancorp   
5   1ST ENTERPRISE BANK                  1st enterprise bank    
6   1ST PACIFIC BANCORP                  1st pacific bancorp    
7   1ST SOURCE CORP                      1st source corporation 
8   1ST UNITED BANCORP INC               1st united bancorp inc 
9   21ST CENTURY ONCOLOGY HLDGS          21st century oncology hldgs

但是,当我创建一个没有要删除的字符串的新变量时,新变量与'nstandar'相同。代码如下:

cemp['newnstandar']=cemp['nstandar'].apply(quitarc)

那么,我的错误是什么?我该如何修复我的代码?

1 个答案:

答案 0 :(得分:0)

最后,我意识到问题在于我的功能。所以我修改它,它的代码是:

def quitarc(x):

    x=''.join(a + " " for a in x.split() if a not in setc)
    x=x.strip()
    return x