簡體   English   中英

獲取單行值的不同計數 Pyspark DataFrame

[英]Get distinct count of values in single row in Pyspark DataFrame

我正在嘗試將字符串列中的逗號分隔值拆分為各個值並計算每個單獨的值。

我的數據格式如下:

+--------------------+
|                tags|
+--------------------+
|cult, horror, got...|
|            violence|
|            romantic|
|inspiring, romant...|
|cruelty, murder, ...|
|romantic, queer, ...|
|gothic, cruelty, ...|
|mystery, suspense...|
|            violence|
|revenge, neo noir...|
+--------------------+

我希望結果看起來像

+--------------------+-----+
|                tags|count|
+--------------------+-----+
|cult                |    4|
|horror              |   10|
|goth                |    4|
|violence            |   30|
...

我試過但沒有用的代碼如下:

data.select('tags').groupby('tags').count().show(10)

我還使用了一個 countdistinct function 也沒有用。

我覺得我需要一個 function 用逗號分隔值然后列出它們但不確定如何執行它們。

您可以使用split()拆分字符串,然后使用explode() 最后,groupby 和計數:

import pyspark.sql.functions as F

df = spark.createDataFrame(data=[
    ["cult,horror"],
    ["cult,comedy"],
    ["romantic,comedy"],
    ["thriler,horror,comedy"],
], schema=["tags"])

df = df \
  .withColumn("tags", F.split("tags", pattern=",")) \
  .withColumn("tags", F.explode("tags"))

df = df.groupBy("tags").count()

[Out]:
+--------+-----+
|tags    |count|
+--------+-----+
|romantic|1    |
|thriler |1    |
|horror  |2    |
|cult    |2    |
|comedy  |3    |
+--------+-----+

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM