在Pyspark数据框中修剪 [英] Trim in a Pyspark Dataframe

查看:92
本文介绍了在Pyspark数据框中修剪的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Pyspark数据框(原始数据框),其中包含以下数据(所有列均具有字符串数据类型).在我的用例中,我不确定此输入数据框中的所有列都在哪.用户只需将数据框的名称传递给我,并要求我修剪此数据框的所有列.典型数据框中的数据如下所示:

I have a Pyspark dataframe(Original Dataframe) having below data(all columns have string datatype). In my use case i am not sure of what all columns are there in this input dataframe. User just pass me the name of dataframe and ask me to trim all the columns of this dataframe. Data in a typical dataframe looks like as below:

  id              Value         Value1
   1               "Text "       "Avb"
   2               1504         " Test"
   3               1               2

无论如何,我都可以做到这一点,而不必依赖于此数据框中存在的所有列,并可以修剪此数据框中所有的列.修剪完数据框的所有列后的数据应看起来像.

Is there anyway i can do it without being dependent on what all columns are present in this dataframe and get all the column trimmed in this dataframe. Data after trimming aall the columns of dataframe should look like.

   id             Value        Value1
   1               "Text"       "Avb"
   2               1504         "Test"
   3               1               2

有人可以帮我吗?如何使用Pyspark数据框实现它?任何帮助将不胜感激.

Can someone help me out? How can i achieve it using Pyspark dataframe? Any help will be appreciated.

推荐答案

在@ osbon123的答案中使用 trim() 函数.

Using trim() function in @osbon123's answer.

from pyspark.sql.functions import trim

for c_name in df.columns:
    df = df.withColumn(c_name, trim(col(c_name)))

这篇关于在Pyspark数据框中修剪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆