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


Python pyspark Index.shift用法及代碼示例

本文簡要介紹 pyspark.pandas.Index.shift 的用法。

用法:

Index.shift(periods: int = 1, fill_value: Optional[Any] = None) → IndexOpsLike

按所需的周期數移動係列/索引。

注意

當前的 shift 實現使用 Spark 的 Window 而不指定分區規範。這會導致將所有數據移動到單個機器中的單個分區中,並可能導致嚴重的性能下降。避免對非常大的數據集使用此方法。

參數

periodsint

要轉移的周期數。可以是正麵的或負麵的。

fill_value對象,可選

用於新引入的缺失值的標量值。默認值取決於 self 的數據類型。對於數值數據,使用np.nan。

返回

輸入係列/索引的副本,已移動。

例子

>>> df = ps.DataFrame({'Col1': [10, 20, 15, 30, 45],
...                    'Col2': [13, 23, 18, 33, 48],
...                    'Col3': [17, 27, 22, 37, 52]},
...                   columns=['Col1', 'Col2', 'Col3'])
>>> df.Col1.shift(periods=3)
0     NaN
1     NaN
2     NaN
3    10.0
4    20.0
Name: Col1, dtype: float64
>>> df.Col2.shift(periods=3, fill_value=0)
0     0
1     0
2     0
3    13
4    23
Name: Col2, dtype: int64
>>> df.index.shift(periods=3, fill_value=0)
Int64Index([0, 0, 0, 0, 1], dtype='int64')

相關用法


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