我有一个spark数据框,其中有一列特别是location_string
,我基本上只想将其分解为3列,分别称为country
、region
和city
。然后我想将这些列与已经存在的country
、region
、city
列合并,以确保填充空值。或者换句话说,我想将我的函数应用于city
、region
或country
为空的行,以尝试使用location_string
填充这些值
示例数据集:
+--------------------+-----------------+------+-------+
| location_string| city|region|country|
+--------------------+-----------------+------+-------+
|Jonesboro, AR, US...| NULL| AR| NULL|
|Lake Village, AR,...| Lake Village| AR| USA|
|Little Rock, AR, ...| Little Rock| AR| USA|
|Little Rock, AR, ...| Little Rock| AR| USA|
|Malvern, AR, US, ...| Malvern| NULL| USA|
|Malvern, AR, US, ...| Malvern| AR| USA|
|Morrilton, AR, US...| Morrilton| AR| USA|
|Morrilton, AR, US...| Morrilton| AR| USA|
|N. Little Rock, A...|North Little Rock| AR| USA|
|N. Little Rock, A...|North Little Rock| AR| USA|
|Ozark, AR, US, 72949| Ozark| AR| USA|
|Ozark, AR, US, 72949| Ozark| AR| USA|
|Palestine, AR, US...| NULL| AR| USA|
|Pine Bluff, AR, U...| Pine Bluff| AR| NULL|
|Pine Bluff, AR, U...| Pine Bluff| AR| USA|
|Prescott, AR, US,...| Prescott| AR| USA|
|Prescott, AR, US,...| Prescott| AR| USA|
|Searcy, AR, US, 7...| Searcy| AR| USA|
|Searcy, AR, US, 7...| Searcy| AR| USA|
|West Memphis, AR,...| West Memphis| NULL| USA|
+--------------------+-----------------+------+-------+
分解位置字符串的示例函数:
def geocoder_decompose_location(location_string):
if not location_string:
return {'country': None, 'state': None, 'city': None}
GOOGLE_GEOCODE_API_KEY = "<API KEY HERE>"
result = geocoder.google(location_string, key=GOOGLE_GEOCODE_API_KEY)
return {'country': result.country, 'state': result.state, 'city': result.city}
scala伪码
首先,我们需要从df中删除所有重复项(这将减少对google服务的API调用)
我们还可以在删除重复项之前执行orderBy(desc(“city”)、desc(“country”)、desc(“state”)),以便在存在重复项时(将删除具有空值的项)
相关问题 更多 >
编程相关推荐