Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用基于regex-Python的另一列值替换列值_Python_Pandas_Numpy - Fatal编程技术网

使用基于regex-Python的另一列值替换列值

使用基于regex-Python的另一列值替换列值,python,pandas,numpy,Python,Pandas,Numpy,这是我的数据帧的摘录 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'],

这是我的数据帧的摘录

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”组成时,“邮政编码”列是错误的

我想做的是:如果列“is_code_postal”是一个“错误”,用列“ADRESE”中出现的邮政编码的正则表达式替换“code_postal”

我找不到解决办法。为此,我尝试了这个
df['is_code_postal']=np.where(df.code_postal.str.match('^[a-zA-z]'),'error','ok')
。起初,我考虑在同一个函数中进行所有更改。但是我错过了一些东西

重要的是我的数据帧有点重(超过25万行),所以我想寻求一个有效的解决方案


你们有什么想法吗?

你们可以忽略邮政编码,直接从“adresse”中提取,使用Quang的代码:

df['code_postal']=df['adresse'].str.extract('(\d{5})')

您可以忽略邮政编码,直接从“ADRESE”中提取,使用来自Quang的代码:

df['code_postal']=df['adresse'].str.extract('(\d{5})')

df['adresse'].str.extract('(\d{5})
对您有用吗?这样做吗
df['is_code_postal']=np.where(df.code_postal.str.match('^[a-zA-z]')、df['adrese'].str.extract('(\d{5}'))、'ok')
@QuangHoang
df['adrese'].str.extract('(\d{5}'))为您提供邮政编码。您可以将它们与
df['code\u postal']
does
df['adresse'].str.extract('(\d{5})
为您工作?这样做吗
df['is_code_postal']=np.where(df.code_postal.str.match('^[a-zA-z]')、df['adrese'].str.extract('(\d{5}'))、'ok')
@QuangHoang
df['adrese'].str.extract('(\d{5}'))为您提供邮政编码。您可以将其与
df['code\u posal']
进行比较,这将覆盖所有邮政编码,您如何将其仅应用于不正确的邮政编码?您可以通过df.loc[df.is\u code\u posal=='error','code\u posal']=df.adresse.str.extract(r'-(\d{5}')仅选择这些行,这将覆盖所有邮政编码,如何将其仅应用于不正确的行?您可以通过df.loc[df.is_code_postail=='error','code_postail']=df.adresse.str.extract(r'\-(\d{5}'))仅选择这些行