我认为你需要 numpy.where 与 Series 完美搭配的东西:
mask = matches['id'].str.contains('M|-0')
matches['url'] = 'http://www.example.org' + matches['column1'] + '/' +
np.where(mask, matches['id'].str[-3:], matches['id'].str[-4:]) + '/xyz.pdf'
示例:
matches = pd.DataFrame({'id':['2010-M012','2010-1234','2010-1234'],
'column1':['s','d','m']})
print (matches)
column1 id
0 s 2010-M012
1 d 2010-1234
2 m 2010-1234
mask = matches['id'].str.contains('M|-0')
matches['url'] = 'http://www.example.org' + matches['column1'] + '/' + \
np.where(mask, matches['id'].str[-3:], matches['id'].str[-4:]) + '/xyz.pdf'
matches['url1'] = 'http://www.example.org' + matches['column1'] + '/' + \
matches['id'].map(lambda x : x[-3:] if (('M' in x) or ('-0' in x)) else x[-4:]) + '/xyz.pdf'
matches['url2'] = matches.apply(lambda x: 'http://www.example.org{}/{}/xyz.pdf'.format(x['column1'], x['id'][-3:] if (('M' in x['id']) or ('-0' in x['id'])) else x['id'][-4:]), axis=1)
print (matches)
column1 id url \
0 s 2010-M012 http://www.example.orgs/012/xyz.pdf
1 d 2010-1234 http://www.example.orgd/1234/xyz.pdf
2 m 2010-1234 http://www.example.orgm/1234/xyz.pdf
url1 url2
0 http://www.example.orgs/012/xyz.pdf http://www.example.orgs/012/xyz.pdf
1 http://www.example.orgd/1234/xyz.pdf http://www.example.orgd/1234/xyz.pdf
2 http://www.example.orgm/1234/xyz.pdf http://www.example.orgm/1234/xyz.pdf
时间安排:
matches = pd.DataFrame({'id':['2010-M012','2010-1234','2010-1234'],
'column1':['s','d','m']})
#[30000 rows x 2 columns]
matches = pd.concat([matches]*10000).reset_index(drop=True)
In [168]: %timeit matches['url'] = 'http://www.example.org' + matches['column1'] + '/' + np.where(matches['id'].str.contains('M|-0'), matches['id'].str[-3:], matches['id'].str[-4:]) + '/xyz.pdf'
10 loops, best of 3: 50.9 ms per loop
In [169]: %timeit matches['url1'] = 'http://www.example.org' + matches['column1'] + '/' + matches['id'].map(lambda x : x[-3:] if (('M' in x) or ('-0' in x)) else x[-4:]) + '/xyz.pdf'
10 loops, best of 3: 22.1 ms per loop
In [170]: %timeit matches['url2'] = matches.apply(lambda x: 'http://www.example.org{}/{}/xyz.pdf'.format(x['column1'], x['id'][-3:] if (('M' in x['id']) or ('-0' in x['id'])) else x['id'][-4:]), axis=1)
1 loop, best of 3: 1.07 s per loop