这是我的DataFrame的摘录
data = [
['Citroën Amillis', '20 Za Des Baliveaux - 77120 Amillis', '77120', 'ok'],
['Relat Paris 9e', 'Métro Opéra - 75009 Paris 9e', 'Paris', 'error'],
['Macif Avon', '49 Av Franklin Roosevelt - 77210 Avon', '77210', 'ok'],
['Atac La Chapelle-la-Reine', 'Za Rue De L\'avenir - 77760 La Chapelle-la-Reine', 'La', 'error'],
['Société Générale La Ferté-Gaucher', '42 Rue De Paris - 77320 La Ferté-Gaucher', 'La', 'error']
]
df = pd.DataFrame(data, columns=['nom_magasin', 'adresse', 'code_postal', 'is_code_postal'])
df
如您所见,我的数据框中存在错误。对于某些地址,尤其是在组成城市名称时(例如:“ La Chapelle-la-Reine”),“ code_postal”列是错误的。
我要执行的操作如下:如果“ is_code_postal”列为“错误”,则将“ code_postal”替换为出现在“ adresse”列中的邮政编码正则表达式。
我找不到解决方案。为此,我已经尝试过了df['is_code_postal'] = np.where(df.code_postal.str.match('^[a-zA-z]'), 'error', 'ok')
。起初,我正在考虑在同一功能内进行所有更改。但是我想念一些东西。
重要的是我的数据帧有点重(超过25万行),所以我想寻求一个有效的解决方案。
你们有什么主意吗?
您可以忽略code_postal并使用Quang中的代码直接从'adresse'中提取它:
df['code_postal']=df['adresse'].str.extract('(\d{5})')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句