2

Looked through the answers to similar queries here but still unsure. Code below produces:

     for i in range(len(df)):
            if df[0]['SubconPartNumber1'].str.isdigit() == False :
                df['SubconPartNumber1'] = df['SubconPartNumber1'].str.replace(',', '/', regex = True)
                df['SubconPartNumber1'] = df['SubconPartNumber1'].str.replace(r"\(.*\)-", '/', regex = True)
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Mehul Gupta
  • 1,829
  • 3
  • 17
  • 33
Tonz
  • 177
  • 1
  • 2
  • 11
  • can u post same dataframe example as in some rows and how is df[0] working either you have 0 as columns or you will get key error because of that. and as a solution you can use apply function – Kunal Sawant Apr 28 '20 at 04:34

2 Answers2

2

In pandas you can avoid loops if possible. Your solution should be replace by boolean mask with ~ for invert instead == False and passed to DataFrame.loc:

m = df['SubconPartNumber1'].str.isdigit()
df.loc[~m, 'SubconPartNumber1'] = df.loc[~m, 'SubconPartNumber1'].str.replace(',', '/', regex = True).str.replace(r"\(.*\)-", '/', regex = True)

But because numeric has only numbers I think mask is not necessary here, also regex should be join by | for or, regex=True is default parameter, so should be omitted:

df = pd.DataFrame({'SubconPartNumber1':['345','aaa,','(bbb)-ccc']})
print (df)
  SubconPartNumber1
0               345
1              aaa,
2         (bbb)-ccc

df['SubconPartNumber1'] = df['SubconPartNumber1'].str.replace(r",|\(.*\)-", '/')
print (df)
  SubconPartNumber1
0               345
1              aaa/
2              /ccc
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

Replace this:

if df[0]['SubconPartNumber1'].str.isdigit() == False :

with this:

if df[0]['SubconPartNumber1'].str.isdigit().empty:

There are other methods besides empty, but I think this will get you what you want.

Trace Malloc
  • 394
  • 1
  • 3
  • 5
  • 1
    Answer is clearly wrong, `==False` is for invert mask or boolean, `.empty` is for test empty Series, DataFrame, so using here it has no sense. – jezrael Apr 28 '20 at 04:29
  • 1
    Clearly you read the error the OP was getting that specifically states to use the `.empty` method? Did you test it? – Trace Malloc Apr 28 '20 at 04:35