简体   繁体   中英

Script not working anymore, .group() used to work but now is throwing an error

This is the portion of the code that's causing trouble:

import pandas as pd
import re

df
df.columns = ['Campaigns', 'Impressions', 'Attempts', 'Spend']
Campaigns = df['Campaigns']
IDs = []
for c in Campaigns:
    num = re.search(r'\d{6}',c).group()
    IDs.append(num)
pieces = [df,pd.DataFrame(IDs)]
frame = pd.concat(pieces, axis=1, join='outer',ignore_index=False)
frame['ID'] = frame[0]
del frame[0]
frame

This is the error:

Error: 'NoneType' object has no attribute 'group'

When I try things individually in ipython everything works, for example:

in>> test = 'YP_WON2_SP8_115436'
in>> num = re.search(r'\d{6}',test)
in>> num.group()
out>> '115436'

I've tried splitting up the code as above and it still throws the same error.

Fixed the code:

df
df.columns = ['Campaigns', 'Impressions', 'Attempts', 'Spend']
Campaigns = df['Campaigns']
ID = []
for c in Campaigns:
    m = re.search(r'\d{6}',c)
    if m:
        num = re.search(r'\d{6}',c).group()
        ID.append(num)
    else:
        ID.append('No ID')
pieces = [df,pd.DataFrame(ID)]
frame = pd.concat(pieces, axis=1, join='outer',ignore_index=False)
frame['ID'] = frame[0]
del frame[0]
frame

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