【问题标题】:Efficiently update columns based on one of the columns split value根据列拆分值之一有效更新列
【发布时间】:2016-07-29 07:54:56
【问题描述】:

所以这是我的代码根据列“位置”的拆分值条件更新许多列值。该代码工作正常,但由于它按行迭代,效率不够高。谁能帮我加快这段代码的运行速度?

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

【问题讨论】:

    标签: python dataframe


    【解决方案1】:

    'map' 是我需要的 :)

    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'])
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-26
      • 1970-01-01
      相关资源
      最近更新 更多