pyspark DataFrame 转RDD
dataframe RDD Pyspark
2023-09-14 09:09:29 时间
# -*- coding: utf-8 -*-
from __future__ import print_function
from pyspark.sql import SparkSession
from pyspark.sql import Row
if __name__ == "__main__":
# 初始化SparkSession
spark = SparkSession \
.builder \
.appName("RDD_and_DataFrame") \
.config("spark.some.config.option", "some-value") \
.getOrCreate()
sc = spark.sparkContext
lines = sc.textFile("employee.txt")
parts = lines.map(lambda l: l.split(","))
employee = parts.map(lambda p: Row(name=p[0], salary=int(p[1])))
#RDD转换成DataFrame
employee_temp = spark.createDataFrame(employee)
#显示DataFrame数据
employee_temp.show()
#创建视图
employee_temp.createOrReplaceTempView("employee")
#过滤数据
employee_result = spark.sql("SELECT name,salary FROM employee WHERE salary >= 14000 AND salary <= 20000")
# DataFrame转换成RDD
result = employee_result.rdd.map(lambda p: "name: " + p.name + " salary: " + str(p.salary)).collect()
#打印RDD数据
for n in result:
print(n)
相关文章
- Spark 2.0 DataFrame map操作中Unable to find encoder for type stored in a Dataset.问题的分析与解决
- 删除重复行的DataFrame
- 根据条件在Pandas DataFrame中选择行
- PySpark的DataFrame处理方法
- xlwings结合dataframe数据的写入
- Python编程语言学习:将多个列表数据保存为dataframe格式数据并按照指定列进行降序排序之详细攻略
- Python:数据类型转换之将Scikit-learn的Bunch数据类型转换为Pandas的DataFrame类型案例及代码实现
- pandas读取文本为Dataframe,保存为csv格式
- Pandas 根据双(多)重索引获取DataFrame指定行数据
- DT tableone各种花里胡哨的图 把dataframe导出成html 把矩阵变成html方便查看 类似于shinny功能 制作三线表等共功能 fextable 花里胡哨的工具 datatable
- Pandas数据处理4、DataFrame记录重复值出现的次数(是总数不是每个值的数量)
- 如何在DataFrame索引某一行