如何将整列的大小写更改为小写?

新手上路,请多包涵

我想在 Spark 数据集中将整列的大小写更改为小写

        Desired Input
        +------+--------------------+
        |ItemID|       Category name|
        +------+--------------------+
        |   ABC|BRUSH & BROOM HAN...|
        |   XYZ|WHEEL BRUSH PARTS...|
        +------+--------------------+

        Desired Output
        +------+--------------------+
        |ItemID|       Category name|
        +------+--------------------+
        |   ABC|brush & broom han...|
        |   XYZ|wheel brush parts...|
        +------+--------------------+

我尝试使用 collectAsList()toString() ,这对于非常大的数据集来说是缓慢而复杂的过程。

我还找到了一种“较低”的方法,但不知道如何让它在 dasaset 中工作。请给我一个简单或有效的方法来完成上述操作。提前致谢

原文由 Shreeharsha 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 431
2 个回答

我明白了(使用 Functions#lower ,参见 Javadoc

import org.apache.spark.sql.functions.lower

         String columnName="Category name";
        src=src.withColumn(columnName, lower(col(columnName)));
        src.show();

这用保留整个数据集的新列替换了旧列。

         +------+--------------------+
        |ItemID|       Category name|
        +------+--------------------+
        |   ABC|brush & broom han...|
        |   XYZ|wheel brush parts...|
        +------+--------------------+

原文由 Shreeharsha 发布,翻译遵循 CC BY-SA 4.0 许可协议

使用 lower 函数 org.apache.spark.sql.functions

例如:

 df.select($"q1Content", lower($"q1Content")).show

输出。

 +--------------------+--------------------+
|           q1Content|    lower(q1Content)|
+--------------------+--------------------+
|What is the step ...|what is the step ...|
|What is the story...|what is the story...|
|How can I increas...|how can i increas...|
|Why am I mentally...|why am i mentally...|
|Which one dissolv...|which one dissolv...|
|Astrology: I am a...|astrology: i am a...|
| Should I buy tiago?| should i buy tiago?|
|How can I be a go...|how can i be a go...|
|When do you use  ...|when do you use  ...|
|Motorola (company...|motorola (company...|
|Method to find se...|method to find se...|
|How do I read and...|how do i read and...|
|What can make Phy...|what can make phy...|
|What was your fir...|what was your fir...|
|What are the laws...|what are the laws...|
|What would a Trum...|what would a trum...|
|What does manipul...|what does manipul...|
|Why do girls want...|why do girls want...|
|Why are so many Q...|why are so many q...|
|Which is the best...|which is the best...|
+--------------------+--------------------+

原文由 Alberto Bonsanto 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题