Python 基于列拆分值之一高效更新列
因此,下面是我的代码,它根据列“location”的拆分值条件更新许多列值。代码运行良好,但由于按行进行迭代,效率不够。有人能帮我把代码写得更快吗Python 基于列拆分值之一高效更新列,python,dataframe,Python,Dataframe,因此,下面是我的代码,它根据列“location”的拆分值条件更新许多列值。代码运行良好,但由于按行进行迭代,效率不够。有人能帮我把代码写得更快吗 for index, row in df.iterrows(): print index location_split =row['location'].split(':') after_county=False after_province=False for l in location_split:
for index, row in df.iterrows():
print index
location_split =row['location'].split(':')
after_county=False
after_province=False
for l in location_split:
if l.strip().endswith('ED'):
df[index, 'electoral_district'] = l
elif l.strip().startswith('County'):
df[index, 'county'] = l
after_county = True
elif after_province ==True:
if l.strip()!='Ireland':
df[index, 'dublin_postal_district'] = l
elif after_county==True:
df[index, 'province'] = l.strip()
after_province = True
“地图”是我需要的:)
def fill_county(column):
res = ''
location_split = column.split(':')
for l in location_split:
if l.strip().startswith('County'):
res= l.strip()
break
return res
df['county'] = map(fill_county, df['location'])