zoukankan      html  css  js  c++  java
  • dataframe去除null、NaN和空字符串

    去除null、NaN

    去除 dataframe 中的 nullNaN 有方法 drop ,用 dataframe.na 找出带有 nullNaN 的行,用 drop 删除行:

    import org.apache.spark.{SparkConf, SparkContext}
    import org.apache.spark.sql.{DataFrame, SQLContext, SparkSession}
    /**
      * Created by TTyb on 2017/10/12.
      */
    object test3 {
      def main(args: Array[String]): Unit = {
        val conf = new SparkConf().setAppName("TTyb").setMaster("local")
        val sc = new SparkContext(conf)
        val spark=new SQLContext(sc)
        val sentenceDataFrame = spark.createDataFrame(Seq(
          (1, "asf"),
          (2, "2143"),
          (3, "rfds"),
          (4, null),
          (5, "")
        )).toDF("label", "sentence")
        sentenceDataFrame.show()
        sentenceDataFrame.na.drop().show()
      }
    }
    

    去除空字符串

    去除空字符串用 dataframe.where

    import org.apache.spark.{SparkConf, SparkContext}
    import org.apache.spark.sql.{DataFrame, SQLContext, SparkSession}
    /**
      * Created by TTyb on 2017/10/12.
      */
    object test3 {
      def main(args: Array[String]): Unit = {
        val conf = new SparkConf().setAppName("TTyb").setMaster("local")
        val sc = new SparkContext(conf)
        val spark=new SQLContext(sc)
        val sentenceDataFrame = spark.createDataFrame(Seq(
          (1, "asf"),
          (2, "2143"),
          (3, "rfds"),
          (4, null),
          (5, "")
        )).toDF("label", "sentence")
        sentenceDataFrame.show()
        // sentenceDataFrame.na.drop().show()
        sentenceDataFrame.where("sentence <> ''").show()
      }
    }
    
  • 相关阅读:
    [转载]datatable中只取前7条数据
    [转载]序列化的作用
    [转载]ASP.NET几种清除页面缓存的方法
    Page_Init()和page_load()区别
    [转载]回调函数
    编程规约
    语法知识【Python核心编程】
    Web基础概念扫盲
    【Tomcat源码调试-1】环境搭建(MyEclipse)
    小希的数表题解
  • 原文地址:https://www.cnblogs.com/TTyb/p/7655129.html
Copyright © 2011-2022 走看看