當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


Python pyspark Series.where用法及代碼示例

本文簡要介紹 pyspark.pandas.Series.where 的用法。

用法:

Series.where(cond: pyspark.pandas.series.Series, other: Any = nan) → pyspark.pandas.series.Series

替換條件為 False 的值。

參數

cond布爾係列

如果 cond 為 True,則保留原始值。如果為 False,則替換為其他對應的值。

other標量,係列

cond 為 False 的條目將替換為來自 other 的相應值。

返回

Series

例子

>>> from pyspark.pandas.config import set_option, reset_option
>>> set_option("compute.ops_on_diff_frames", True)
>>> s1 = ps.Series([0, 1, 2, 3, 4])
>>> s2 = ps.Series([100, 200, 300, 400, 500])
>>> s1.where(s1 > 0).sort_index()
0    NaN
1    1.0
2    2.0
3    3.0
4    4.0
dtype: float64
>>> s1.where(s1 > 1, 10).sort_index()
0    10
1    10
2     2
3     3
4     4
dtype: int64
>>> s1.where(s1 > 1, s1 + 100).sort_index()
0    100
1    101
2      2
3      3
4      4
dtype: int64
>>> s1.where(s1 > 1, s2).sort_index()
0    100
1    200
2      2
3      3
4      4
dtype: int64
>>> reset_option("compute.ops_on_diff_frames")

相關用法


注:本文由純淨天空篩選整理自spark.apache.org大神的英文原創作品 pyspark.pandas.Series.where。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。