我想做非常简单的事情,但无法弄清楚如何在 Python/Spark(1.5)/Dataframe 中做到这一点(这对我来说是全新的)。
原始数据集:
code| ISO | country
1 | AFG | Afghanistan state
2 | BOL | Bolivia Plurinational State
新数据集:
code| ISO | country
1 | AFG | Afghanistan
2 | BOL | Bolivia
我想做这样的事情(在伪 Python 中?):
iso_to_country_dict = {'AFG': 'Afghanistan', 'BOL': 'Bolivia'}
def mapCountry(iso,country):
if(iso_to_country_dict[iso] is not empty):
return iso_to_country_dict[iso]
return country
dfg = df.select(mapCountry(df['ISO'],df['country']))
为简单起见, mapCountry 可能如下所示:
def mapCountry(iso,country):
if(iso=='AFG'):
return 'Afghanistan'
return country
但这是有错误的:ValueError: Cannot convert column into bool: