Devo eliminare gli accenti dai caratteri in spagnolo e altre lingue da diversi set di dati.Qual è il modo migliore per rimuovere gli accenti con i datafram di scintilla di apache in PySpark?
Ho già eseguito una funzione basata sul codice fornito in questo post che rimuove gli accenti speciali. Il problema è che la funzione è lenta perché usa uno UDF
. Mi chiedo se posso migliorare le prestazioni della mia funzione per ottenere risultati in meno tempo, perché ciò è positivo per i piccoli dataframes ma non per quelli grandi.
Grazie in anticipo.
Ecco il codice, si sarà in grado di farlo funzionare come si presenta:
# Importing sql types
from pyspark.sql.types import StringType, IntegerType, StructType, StructField
from pyspark.sql.functions import udf, col
import unicodedata
# Building a simple dataframe:
schema = StructType([StructField("city", StringType(), True),
StructField("country", StringType(), True),
StructField("population", IntegerType(), True)])
countries = ['Venezuela', '[email protected]', 'Brazil', 'Spain']
cities = ['Maracaibó', 'New York', ' São Paulo ', '~Madrid']
population = [37800000,19795791,12341418,6489162]
# Dataframe:
df = sqlContext.createDataFrame(list(zip(cities, countries, population)), schema=schema)
df.show()
class Test():
def __init__(self, df):
self.df = df
def clearAccents(self, columns):
"""This function deletes accents in strings column dataFrames,
it does not eliminate main characters, but only deletes special tildes.
:param columns String or a list of column names.
"""
# Filters all string columns in dataFrame
validCols = [c for (c, t) in filter(lambda t: t[1] == 'string', self.df.dtypes)]
# If None or [] is provided with column parameter:
if (columns == "*"): columns = validCols[:]
# Receives a string as an argument
def remove_accents(inputStr):
# first, normalize strings:
nfkdStr = unicodedata.normalize('NFKD', inputStr)
# Keep chars that has no other char combined (i.e. accents chars)
withOutAccents = u"".join([c for c in nfkdStr if not unicodedata.combining(c)])
return withOutAccents
function = udf(lambda x: remove_accents(x) if x != None else x, StringType())
exprs = [function(col(c)).alias(c) if (c in columns) and (c in validCols) else c for c in self.df.columns]
self.df = self.df.select(*exprs)
foo = Test(df)
foo.clearAccents(columns="*")
foo.df.show()
Dopo aver lavorato in questa funzione, il resto del codice può essere trovato in [libreria di trasformazione] (https://github.com/mood-agency/optimus) –