仅从 pyspark 中的 Spark DF 中选择数字/字符串列名称 [英] Selecting only numeric/string columns names from a Spark DF in pyspark

查看:16
本文介绍了仅从 pyspark 中的 Spark DF 中选择数字/字符串列名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 pyspark (2.1.0) 中有一个 SparkDataFrame,我希望仅获取数字列的名称或仅获取字符串列的名称.

I have a SparkDataFrame in pyspark (2.1.0) and I am looking to get the names of numeric columns only or string columns only.

例如,这是我的 DF 的架构:

For example, this is the Schema of my DF:

root
 |-- Gender: string (nullable = true)
 |-- SeniorCitizen: string (nullable = true)
 |-- MonthlyCharges: double (nullable = true)
 |-- TotalCharges: double (nullable = true)
 |-- Churn: string (nullable = true)

这就是我需要的:

num_cols = [MonthlyCharges, TotalCharges]
str_cols = [Gender, SeniorCitizen, Churn]

我该怎么做?谢谢!

推荐答案

dtypes 是可以使用简单过滤器的元组列表 (columnNane,type)

dtypes is list of tuples (columnNane,type) you can use simple filter

 columnList = [item[0] for item in df.dtypes if item[1].startswith('string')]

这篇关于仅从 pyspark 中的 Spark DF 中选择数字/字符串列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆