【问题标题】:How I can select a column where in another column I need a specific things如何在另一列中选择需要特定内容的列
【发布时间】:2021-04-23 17:54:26
【问题描述】:

我有一个 pyspark 数据框。如何在另一列中选择需要特定内容的列。假设我有 n 列。我有 2 列

A.  B.
a   b 
a   c
d   f

我想要所有 B 列。其中 A 列是 a。所以

A.  B.
a   b 
a   c
 

【问题讨论】:

  • 使用df.filter("A='a'")

标签: sql dataframe apache-spark pyspark apache-spark-sql


【解决方案1】:

这只是一个简单的filter

df2 = df.filter("A = 'a'")

它有多种口味,例如

df2 = df.filter(df.A == 'a')
df2 = df.filter(df['A'] == 'a')

import pyspark.sql.functions as F
df2 = df.filter(F.col('A') == 'a')

【讨论】:

  • 之后我可以选择B
  • 您可以使用 df2.B 或 df2['B'] @elham 选择 B
  • AnalysisException: 由于数据类型不匹配,无法解析 '(workexperiences.location = 'US')':'(workexperiences.location = 'US') 中的不同类型' (数组 和字符串).;第 1 行 pos 0;
  • @elham 您正在尝试使用单个字符串过滤数组类型列。您不能将数组与字符串进行比较。如果要过滤包含特定字符串的数组的行,可以执行df2 = df.filter("array_contains(workexperiences.location, 'US')")
猜你喜欢
  • 2015-06-15
  • 1970-01-01
  • 1970-01-01
  • 2012-06-21
  • 2020-04-22
  • 1970-01-01
  • 2019-11-11
  • 2013-01-02
  • 2018-01-28
相关资源
最近更新 更多