在打印AllTogetherNoNan_df.head()
0 Null Null Null Misty Morn Null Horsefen Road Great Yarmouth NR29 5QJ Ludham Null
1 Null Null 16 St Catherines Way Null Gorleston Norfolk NR31 7QB Great Yarmouth Null
3 Null Null 21 Links Road Null Gorleston Norfolk NR31 6JP Great Yarmouth Null
4 Null Null Null Ground Floor Null Back Flat Gorleston NR31 6AL 15 Upper Cliff Road Null
6 Null Null 3 The Boulters Null Gorleston Norfolk NR31 6TF Great Yarmouth Null
我想切割诺福克郡,并将其粘贴到新的“县”栏中
答案 0 :(得分:0)
您可以分两步进行
如果列字符串具有“ Norfolk”子字符串,则复制“ Norfolk”
剥离原始列。
def copy(row):
if 'Norfolk' in row[col_index_in_question]:
return 'Norfolk'
def strip(row):
return row[col_index_in_question].replace('Norfolk', '')
df['County'] = df.apply(copy, axis=1)
df[col_index_in_question] = df.apply(strip, axis=1)
用列索引替换col_index_in_question
但更重要的是,如果它是您要解析的特定格式的地址,则可以使用此Python Parser Lib