- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这是我的数据框:
from pyspark.sql import SparkSession
from pyspark.sql import functions as F
spark = SparkSession.builder.getOrCreate()
dCols = ['c1', 'c2']
dData = [('a', 'b'),
('c', 'd'),
('e', None)]
df = spark.createDataFrame(dData, dCols)
.isin()
中是否有包含null
的语法?
有点像
df = df.withColumn(
'newCol',
F.when(F.col('c2').isin({'d', None}), 'true') # <=====?
.otherwise('false')
).show()
执行代码后得到
+---+----+------+
| c1| c2|newCol|
+---+----+------+
| a| b| false|
| c| d| true|
| e|null| false|
+---+----+------+
代替
+---+----+------+
| c1| c2|newCol|
+---+----+------+
| a| b| false|
| c| d| true|
| e|null| true|
+---+----+------+
我想找到一个不需要引用同一列两次的解决方案,而我们现在需要这样做:
(F.col('c2') == 'd') | F.col('c2').isNull()
最佳答案
在这种情况下,对该列的一个引用是不够的。要检查空值,您需要使用单独的 isNull
方法。
此外,如果你想要一列true/false
,你可以直接将结果转换为 bool 值,而无需使用when
:
import pyspark.sql.functions as F
df2 = df.withColumn(
'newCol',
(F.col('c2').isin(['d']) | F.col('c2').isNull()).cast('boolean')
)
df2.show()
+---+----+------+
| c1| c2|newCol|
+---+----+------+
| a| b| false|
| c| d| true|
| e|null| true|
+---+----+------+
关于python - 在 PySpark isin 中包含 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66209128/
我正在使用 Pandas 和 Python 导入一个 CSV,并对导入的数据框中的数据进行操作,以便创建一个新列。 新列中的每一行都是基于 A 列和 B 列的每个对应行中的值生成的。数据框中有更多包含
我的数据框是这样的: userid codeassigned timestamp 15 553938
我想从一个非常奇怪的字符串中解析出一个 ISIN,我的代码如下所示: > df dput(df) structure(list(ID = c(1L, 2L, 4L, 2L, 3L, 24L), VA
我对 isin() 函数准确性有一些问题。 我的 DataFrame 中有 abc 许多 ID: df = DataFrame[DataFrame['id'].isin(IDs)] 但是结果: pri
我有一本字典,我想使用该字典对 df 中的新列进行分类。 df 中的 Value 列应该与字典中的值进行比较。 df 中的新列应该是与值关联的键。 d = {'Car':['1','2','3'],
我正在尝试检查另一个数据框中是否存在行。我没有加入/合并,因为它会产生重复,然后需要过滤掉重复可能也会过滤掉我想要保留的实际重复。 示例: table1 = pd.DataFrame({'a':[1,
我正在尝试使用 Java 中的列表来过滤 Spark DataFrame。 java.util.List selected = ....; DataFrame result = df.filter(d
我有两个数据框。 Dataframe A 有一列由 list ids(命名项)值组成。数据框 B 有一列 int id 值(命名为 id)。 数据框 A: date | items
我正在尝试将 .isin 与 ~ 一起使用,这样我就可以根据 2 个数据集中的多列获取唯一行的列表。 所以,我有 2 个 9 行的数据集:df1 是底部,df2 是顶部(抱歉,但我无法让它在下面同时显
d = {'Dates':[pd.Timestamp('2013-01-02'), pd.Timestamp('2013-01-03'), pd
isin() 给我奇怪的结果。我创建了以下 DataFrame: import pandas as pd import numpy as np test=pd.DataFrame({'1': np.l
我有一个非常简单的场景,我想测试二维数组的两个元素是否(单独)是更大数组的成员 - 例如: full_array = np.array(['A','B','C','D','E','F']) sub_a
我可以帮助您使用 ISIN pandas 函数。基本上,我需要根据不同的标准按年汇总数据框中的数据。问题是我需要对数据进行许多聚合(例如国家名称、资助计划等)。为了方便起见,我试图在 for 循环中执
这是我的模型: type: { type: Sequelize.STRING, defaultValue: 'text', allowNull: fal
我有两个数组: a = np.array([[1, 2], [3, 4], [5, 6]]) b = np.array([[1, 1, 1, 3, 3], [1, 2, 4
这是一个例子 创建一个包含 100M 相同行的表: >>> df = pd.DataFrame([('20170101', 'TULIP', 'FLOWER')] * 100000000, colum
这个问题已经有答案了: check for identical rows in different numpy arrays (7 个回答) 已关闭 2 年前。 我有两个数组: A = np.arra
假设我有以下两个数据框: df = pd.DataFrame({'col1':['a','b', 'c'], 'col2': ['q', 'w', 'e']}) df1 = pd.DataFrame(
这个问题已经有答案了: check for identical rows in different numpy arrays (7 个回答) 已关闭 2 年前。 我有两个数组: A = np.arra
假设我有以下两个数据框: df = pd.DataFrame({'col1':['a','b', 'c'], 'col2': ['q', 'w', 'e']}) df1 = pd.DataFrame(
我是一名优秀的程序员,十分优秀!