2017-04-03 86 views
1

我有一个包含一个地点(街道名称,x和y坐标)以下列:分割列到两列(熊猫)

Location 
"1139 57 STREET New York (40.632653207600001, -74.000244990799999)" 

我想要做的就是把它分解成三列: '地址','经度'和'纬度'。类似于:

Location     Latitude    Longitude 
"1139 57 STREET New York 40.632653207600001 -74.000244990799999" 

我该如何去做这件事?

+0

的可能的复制[熊猫拆柱(HTTP://计算器的.com /问题/ 36052257 /大熊猫系分割列) –

回答

1

使用str.extract

df.Location.str.extract(
    '^(?P<Location>.*)\s*\((?P<Latitude>[^,]*),\s*(?P<Longitude>\S*)\).*$', 
    expand=True 
) 

        Location   Latitude   Longitude 
0 1139 57 STREET New York 40.632653207600001 -74.000244990799999 
0

不使用正则表达式,假设你的原始数据格式一致另一个想法:

def split_location(row): 

    Location = row[:row.find('(')-1] 
    Latitude = row[row.find('(')+1 : r.find(',')] 
    Longitude = row[row.find(',')+2 :-1] 

    return {'Location' : Location, 
      'Latitude' : Latitude, 
      'Longitude' : Longitude} 

# original_df is a 1 column dataframe of Location (string) that you want to split 
split_df = original_df[Location].apply(lambda x: split_location(x)) 
split_df = pd.DataFrame(list(split_df))