How to get a value from the Row object in Spark Dataframe?

后端 未结 3 788
刺人心
刺人心 2020-12-06 09:49

for

averageCount = (wordCountsDF
                .groupBy().mean()).head()

I get

Row(avg(count)=1.6666666666666667)

相关标签:
3条回答
  • 2020-12-06 10:31

    This also works:

    averageCount = (wordCountsDF
                    .groupBy().mean('count').collect())[0][0]
    print averageCount
    
    0 讨论(0)
  • 2020-12-06 10:40

    Dataframe rows are inherited from namedtuples (from the collections library), so while you can index them like a traditional tuple the way you did above, you probably want to access it by the name of its fields. That is, after all, the point of named tuples, and it is also more robust to future changes. Like this:

    averageCount = wordCountsDF.groupBy().mean().head()['avg(jobs)']
    
    0 讨论(0)
  • 2020-12-06 10:41

    I figured it out. This will return me the value:

    averageCount = (wordCountsDF
                    .groupBy().mean()).head()[0]
    
    0 讨论(0)
提交回复
热议问题