简体   繁体   English

如何获得不同的值,dataframe 中的一列计数并使用 Spark2 和 Scala

[英]How to get distinct value, count of a column in dataframe and store in another dataframe as (k,v) pair using Spark2 and Scala

I want to get the distinct values and their respective counts of every column of a dataframe and store them as (k,v) in another dataframe.我想获取 dataframe 的每一列的不同值及其各自的计数,并将它们作为 (k,v) 存储在另一个 dataframe 中。 Note: My Columns are not static, they keep changing.注意:我的列不是 static,它们一直在变化。 So, I cannot hardcore the column names instead I should loop through them.所以,我不能硬核列名,而是应该循环遍历它们。

For Example, below is my dataframe例如,下面是我的 dataframe

+----------------+-----------+------------+
|name            |country    |DOB         |
+----------------+-----------+------------+
|       Blaze    |        IND|    19950312|
|       Scarlet  |        USA|    19950313|
|       Jonas    |        CAD|    19950312|
|       Blaze    |        USA|    19950312|
|       Jonas    |        CAD|    19950312|
|       mark     |        USA|    19950313|
|       mark     |        CAD|    19950313|
|       Smith    |        USA|    19950313|
|       mark     |        UK |    19950313|
|       scarlet  |        CAD|    19950313|

My final result should be created in a new dataframe as (k,v) where k is the distinct record and v is the count of it.我的最终结果应该在一个新的 dataframe 作为 (k,v) 中创建,其中 k 是不同的记录,v 是它的计数。

+----------------+-----------+------------+
|name            |country    |DOB         |
+----------------+-----------+------------+
|   (Blaze,2)    |   (IND,1) |(19950312,3)|
|   (Scarlet,2)  |   (USA,4) |(19950313,6)|
|   (Jonas,3)    |   (CAD,4) |            |
|   (mark,3)     |   (UK,1)  |            |
|   (smith,1)    |           |            |

Can anyone please help me with this, I'm using Spark 2.4.0 and Scala 2.11.12谁能帮我解决这个问题,我正在使用 Spark 2.4.0 和 Scala 2.11.12

Note: My columns are dynamic, so I can't hardcore the columns and do groupby on them.注意:我的列是动态的,所以我不能对列进行硬核化并对它们进行分组。

I don't have exact solution to your query but I can surely provide you with some help that can get you started working on your issue.我对您的查询没有确切的解决方案,但我肯定可以为您提供一些帮助,帮助您开始解决您的问题。

Create dataframe创建 dataframe

scala> val df = Seq(("Blaze  ","IND","19950312"),
     | ("Scarlet","USA","19950313"),
     | ("Jonas  ","CAD","19950312"),
     | ("Blaze  ","USA","19950312"),
     | ("Jonas  ","CAD","19950312"),
     | ("mark   ","USA","19950313"),
     | ("mark   ","CAD","19950313"),
     | ("Smith  ","USA","19950313"),
     | ("mark   ","UK ","19950313"),
     | ("scarlet","CAD","19950313")).toDF("name", "country","dob")

Next calculate count of distinct element of each column接下来计算每列的不同元素的计数

scala> val distCount = df.columns.map(c => df.groupBy(c).count)

Create a range to iterate over distCount创建一个范围以迭代 distCount

scala> val range = Range(0,distCount.size)
range: scala.collection.immutable.Range = Range(0, 1, 2)

Aggregate your data汇总您的数据

scala> val aggVal = range.toList.map(i => distCount(i).collect().mkString).toSeq
aggVal: scala.collection.immutable.Seq[String] = List([Jonas  ,2][Smith  ,1][Scarlet,1][scarlet,1][mark   ,3][Blaze  ,2], [CAD,4][USA,4][IND,1][UK ,1], [19950313,6][19950312,4])

Create data frame:创建数据框:

scala> Seq((aggVal(0),aggVal(1),aggVal(2))).toDF("name", "country","dob").show()

+--------------------+--------------------+--------------------+
|                name|             country|                 dob|
+--------------------+--------------------+--------------------+
|[Jonas  ,2][Smith...|[CAD,4][USA,4][IN...|[19950313,6][1995...|
+--------------------+--------------------+--------------------+

I hope this helps you in some way.我希望这对您有所帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将 dataframe 转换为 dict,使用列名作为键,另一列作为 k,v - python,pandas,Z6A55074B5CDF47549 - convert dataframe to dict, using a column name as key and another columns as k, v - python, pandas, dataframe Pandas:如何在由另一列分组的列上获取具有最大值 value_count 的行作为数据框 - Pandas: how to get the rows that has the maximum value_count on a column grouping by another column as a dataframe dataframe 列中的不同字符串计数 - Distinct string count in dataframe column 使用 Pandas 根据数据框中的另一列值获取特定值的计数和总数 - Get count of particular values and the total based on another column value in dataframe using Pandas 计算熊猫数据框中的不同值 - count distinct value in pandas dataframe 如何计算数据框中另一列中每个唯一值对应的值? - How to count the values corresponding to each unique value in another column in a dataframe? 如何从示例数据帧的 created_time 列中获取小时数并将其作为另一个数据帧计数 - How can I get the Hours from the column created_time of sample dataframe and get count of it as another dataframe 如何获得匹配数据框中的一列的值与另一列 - How to get matches the value of one column with another column in the dataframe 使用数据框列中的键对值创建新列 - Create new column using keys pair value from a dataframe column Pandas,获取数据帧列中单个值的计数 - Pandas, Get count of a single value in a Column of a Dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM