|CallID| Customer | Response |
+------+----------------------------------+------------------------------------+
| 1 |Ready to repay the amount. |He is ready to pay $50 by next week.|
| 2 |Mr. John's credit card is blocked.|Asked to verify last 3 transactions.|
| 3 |Mr. Tom is unable to pay bills. |Asked to verify registered email add|
+------+----------------------------------+------------------------------------
Я выбираю отдельные столбцы, выполняю исправление орфографии и соединяю их обратно.Вот мой код:
1.Выбор отдельных столбцов
from textblob import TextBlob
from itertools import islice
from pyspark.sql.functions import monotonically_increasing_id, col, asc
t = df.count()
newColumns = df.schema.names
df_t = df.select(df['Customer'])
s1 = ''
for i in range(t):
rdd = df_t.rdd
s = str(rdd.collect()[i][0])
s1 = s1 + '|' + s
text = str(TextBlob(s1).correct())
l = text.split('|')
rdd2 = sc.parallelize(l)
df1 = rdd2.map(lambda x: (x,)) \
.mapPartitionsWithIndex(lambda idx, it: islice(it, 1, None) if idx == 0 else
it) \
.toDF([newColumns[1]])
s = s1 = rdd = rdd2 = text = ''
l = []
df_t = df.select(df['Response'])
for i in range(t):
rdd = df_t.rdd
s = str(rdd.collect()[i][0])
s1 = s1 + '|' + s
text = str(TextBlob(s1).correct())
l = text.split('|')
rdd2 = sc.parallelize(l)
df2 = rdd2.map(lambda x: (x,)) \
.mapPartitionsWithIndex(lambda idx, it: islice(it, 1, None) if idx == 0 else
it) \
.toDF([newColumns[2]])`
2.Присоединение к ним обратно
df1 = df1.withColumn("id", monotonically_increasing_id())
df2 = df2.withColumn("id", monotonically_increasing_id())
dffinal = df2.join(df1, "id", "outer").orderBy('id',
ascending=True).drop("id")
3.Окончательный результат
| Customer | Response |
+----------------------------------+------------------------------------+
|Ready to repay the amount. |He is ready to pay $50 by next week.|
|Mr. John's credit card is blocked.|Asked to verify last 3 transactions.|
|Mr. Tom is unable to pay bills. |Asked to verify registered email add|
+----------------------------------+------------------------------------+
Это хороший подход, если у нас меньше столбцов.Но есть ли способ написать обобщенный код, в котором мы могли бы создавать DataFrames и объединять их, не используя столбцы, как массив или список элементов?