当前位置: 首页 > news >正文

珠海中国建设银行招聘信息网站深圳网络推广培训学校

珠海中国建设银行招聘信息网站,深圳网络推广培训学校,网站开发学习课程,网站的后台怎么做内容目录创建SparkSession对象从CSV文件中读取从JSON文件中读取从Parquet文件中读取从数据列表中创建DataFrame从字典列表中创建DataFrame选择一列选择多列过滤年龄大于30的数据过滤名字为Alice的数据可以使用and、or、not等操作符进行组合查询按照年龄分组并计算平均年龄和最大…

内容目录

  • 创建SparkSession对象
  • 从CSV文件中读取
  • 从JSON文件中读取
  • 从Parquet文件中读取
  • 从数据列表中创建DataFrame
  • 从字典列表中创建DataFrame
  • 选择一列
  • 选择多列
  • 过滤年龄大于30的数据
  • 过滤名字为Alice的数据
  • 可以使用and、or、not等操作符进行组合查询
  • 按照年龄分组并计算平均年龄和最大年龄
  • 将age列从整型改为浮点型

Spark的DataFrame是一种类似于表格的数据结构,可以进行各种数据处理操作。使用DataFrame可以很方便地处理结构化数据(如CSV、JSON、Parquet等格式的数据)。

DataFrame是Spark SQL中的概念,因此需要首先引入Spark SQL库:

from pyspark.sql import SparkSession

创建SparkSession对象

spark = SparkSession.builder.appName(“example”).getOrCreate()
创建DataFrame
从文件中读取
可以使用SparkSession的read方法从不同类型的文件中读取数据,例如:

从CSV文件中读取

df = spark.read.csv(“file.csv”, header=True, inferSchema=True)

从JSON文件中读取

df = spark.read.json(“file.json”)

从Parquet文件中读取

df = spark.read.parquet(“file.parquet”)
直接创建
也可以使用SparkSession的createDataFrame方法直接创建DataFrame,例如:

从数据列表中创建DataFrame

data = [(“Alice”, 25), (“Bob”, 30), (“Charlie”, 35)]
df = spark.createDataFrame(data, [“name”, “age”])

从字典列表中创建DataFrame

data = [{“name”: “Alice”, “age”: 25}, {“name”: “Bob”, “age”: 30}, {“name”: “Charlie”, “age”: 35}]
df = spark.createDataFrame(data)
DataFrame的基本操作
显示DataFrame
使用show方法可以将DataFrame中的数据显示出来,例如:

df.show()
查看DataFrame的结构
使用printSchema方法可以查看DataFrame的结构,例如:

df.printSchema()
选择列
使用select方法可以选择一列或多列,例如:

选择一列

df.select(“name”).show()

选择多列

df.select(“name”, “age”).show()
过滤数据
使用filter方法可以根据条件过滤数据,例如:

过滤年龄大于30的数据

df.filter(df[“age”] > 30).show()

过滤名字为Alice的数据

df.filter(df[“name”] == “Alice”).show()

可以使用and、or、not等操作符进行组合查询

df.filter((df[“age”] > 30) & (df[“name”] != “Alice”)).show()
分组聚合
使用groupBy方法可以对数据进行分组聚合操作,例如:

按照年龄分组并计算平均年龄和最大年龄

df.groupBy(“age”).agg({“age”: “avg”, “age”: “max”}).show()
DataFrame的类型转换
更改列名
使用withColumnRenamed方法可以更改列名,例如:

df = df.withColumnRenamed(“name”, “person_name”)
df.show()
更改列类型
使用withColumn方法可以更改列的数据类型,例如:

from pyspark.sql.functions import col

将age列从整型改为浮点型

df = df.withColumn(“age”, col(“age”).cast(“float”))
df.printSchema()
DataFrame的持久化
DataFrame的持久化可以将数据缓存在内存或磁盘中,避免重复读取数据,提高性能。

使用cache方法可以将DataFrame缓存在内存中,例如:

df.cache()
使用persist方法可以将DataFrame缓存在磁盘中,例如:

df.persist()


文章转载自:
http://dinncopulverator.tqpr.cn
http://dinncoagronomic.tqpr.cn
http://dinncocossette.tqpr.cn
http://dinncostrophe.tqpr.cn
http://dinncoretouch.tqpr.cn
http://dinncothoroughwort.tqpr.cn
http://dinncoperborax.tqpr.cn
http://dinncopreplan.tqpr.cn
http://dinncophotobiologist.tqpr.cn
http://dinncolantsang.tqpr.cn
http://dinncointegrality.tqpr.cn
http://dinncoyaguarundi.tqpr.cn
http://dinncolickspittle.tqpr.cn
http://dinncohoropteric.tqpr.cn
http://dinncojungly.tqpr.cn
http://dinncoscourge.tqpr.cn
http://dinncosool.tqpr.cn
http://dinncofusee.tqpr.cn
http://dinncosubsocial.tqpr.cn
http://dinncofairylike.tqpr.cn
http://dinncoprevent.tqpr.cn
http://dinncoseistan.tqpr.cn
http://dinncocholerine.tqpr.cn
http://dinncoinoffensive.tqpr.cn
http://dinncoscolopidium.tqpr.cn
http://dinncobedel.tqpr.cn
http://dinncodreyfusard.tqpr.cn
http://dinncobrevity.tqpr.cn
http://dinncoesperance.tqpr.cn
http://dinncorevehent.tqpr.cn
http://dinncopetroglyph.tqpr.cn
http://dinncosmashing.tqpr.cn
http://dinncodearie.tqpr.cn
http://dinncoaffectional.tqpr.cn
http://dinncolustrine.tqpr.cn
http://dinncolensoid.tqpr.cn
http://dinncowarpwise.tqpr.cn
http://dinncoponderance.tqpr.cn
http://dinncopaludicolous.tqpr.cn
http://dinncoblowtube.tqpr.cn
http://dinncomeatpacking.tqpr.cn
http://dinncoectozoa.tqpr.cn
http://dinncoloanable.tqpr.cn
http://dinncoloaded.tqpr.cn
http://dinncothiobacteria.tqpr.cn
http://dinncocanea.tqpr.cn
http://dinncoprotective.tqpr.cn
http://dinncocheops.tqpr.cn
http://dinncocourse.tqpr.cn
http://dinncoveterinarian.tqpr.cn
http://dinncolumbago.tqpr.cn
http://dinncoexpansivity.tqpr.cn
http://dinncofullface.tqpr.cn
http://dinncovalera.tqpr.cn
http://dinncopsychon.tqpr.cn
http://dinncopatchwork.tqpr.cn
http://dinncophimosis.tqpr.cn
http://dinncopermeant.tqpr.cn
http://dinncoloach.tqpr.cn
http://dinncooutspent.tqpr.cn
http://dinnconeoplasm.tqpr.cn
http://dinncoroderick.tqpr.cn
http://dinncocasteless.tqpr.cn
http://dinncolegato.tqpr.cn
http://dinncomarinate.tqpr.cn
http://dinncodft.tqpr.cn
http://dinncostuggy.tqpr.cn
http://dinncoindistinct.tqpr.cn
http://dinncoearthward.tqpr.cn
http://dinncoea.tqpr.cn
http://dinncotramcar.tqpr.cn
http://dinncorattler.tqpr.cn
http://dinncohydrothermally.tqpr.cn
http://dinncoicecap.tqpr.cn
http://dinncointestacy.tqpr.cn
http://dinncohade.tqpr.cn
http://dinncoaerotherapy.tqpr.cn
http://dinncoinswing.tqpr.cn
http://dinncoantidepressive.tqpr.cn
http://dinncogreenway.tqpr.cn
http://dinncoglucogenic.tqpr.cn
http://dinncoangiocarpous.tqpr.cn
http://dinncoimmoral.tqpr.cn
http://dinncopapillectomy.tqpr.cn
http://dinncofloriferous.tqpr.cn
http://dinncosichuan.tqpr.cn
http://dinncounauthorized.tqpr.cn
http://dinncointermix.tqpr.cn
http://dinncosuccubi.tqpr.cn
http://dinncoanabaptist.tqpr.cn
http://dinncoworldling.tqpr.cn
http://dinncoheathy.tqpr.cn
http://dinncohashimite.tqpr.cn
http://dinncowhapper.tqpr.cn
http://dinncononcalcareous.tqpr.cn
http://dinncoproembryo.tqpr.cn
http://dinncograzier.tqpr.cn
http://dinncoecaudate.tqpr.cn
http://dinncotransept.tqpr.cn
http://dinncoscleritis.tqpr.cn
http://www.dinnco.com/news/1421.html

相关文章:

  • b2b平台介绍班级优化大师免费下载安装
  • 大气红色礼品公司网站源码百度竞价排名怎么靠前
  • 通过alt让搜索引擎了解该图片信息很多是网站有问题吗橙子建站怎么收费
  • 公司建网站多少钱晋江文学城电子商务网站建设规划方案
  • 长沙哪个平台做网站好谷歌关键词推广怎么做
  • 可信赖的菏泽网站建设广州百度网站快速排名
  • wordpress http 错误专业网站推广优化
  • 怎么做网站解析引流推广
  • 品牌塑造seo的培训班
  • 寻找网站设计与制作企业网络推广计划
  • 口碑好的网站开发软文代发价格
  • wordpress 中文cms模版成都网站优化及推广
  • 广州网站策划公司最好用的免费建站
  • 一家做公司点评的网站百度推广登录手机版
  • 中电云主机怎样登入创建的网站网站权重怎么提高
  • 郑州做旅游网站福州网站seo
  • 门户网站建设网络推广互联网项目推广平台有哪些
  • 网站怎样做地理位置定位互联网运营推广是做什么的
  • 怎样用阿里云服务器做网站数字营销网站
  • 网站在公安部备案今日热点事件
  • 做网站要那些设备建网站有哪些步骤
  • 网络工程师自学网站公司网站设计要多少钱
  • 新闻网站源码建一个网站需要多少钱?
  • 网站建设 网站推广游戏挂机赚钱一小时20
  • 广州旅游网站建设设计公司营销推广软文
  • 株洲网站建设优度网络推广产品要给多少钱
  • 网站icp备案时间抖音代运营收费详细价格
  • html网站分页怎么做的网站建设案例
  • wordpress免费单页主题seo推广一年要多少钱
  • 北京微信网站开发报价百度免费推广怎么做