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


Python pyspark DataFrame.astype用法及代碼示例

本文簡要介紹 pyspark.pandas.DataFrame.astype 的用法。

用法:

DataFrame.astype(dtype: Union[str, numpy.dtype, pandas.core.dtypes.base.ExtensionDtype, Dict[Union[Any, Tuple[Any, …]], Union[str, numpy.dtype, pandas.core.dtypes.base.ExtensionDtype]]]) → pyspark.pandas.frame.DataFrame

將 pandas-on-Spark 對象轉換為指定的 dtype dtype

參數

dtype數據類型,或列名的字典 -> 數據類型

使用 numpy.dtype 或 Python 類型將整個 pandas-on-Spark 對象轉換為相同類型。或者,使用 {col: dtype, …},其中 col 是列標簽,dtype 是 numpy.dtype 或 Python 類型,將 DataFrame 的一個或多個列轉換為 column-specific 類型。

返回

casted與調用者相同的類型

例子

>>> df = ps.DataFrame({'a': [1, 2, 3], 'b': [1, 2, 3]}, dtype='int64')
>>> df
   a  b
0  1  1
1  2  2
2  3  3

轉換為浮點類型:

>>> df.astype('float')
     a    b
0  1.0  1.0
1  2.0  2.0
2  3.0  3.0

轉回 int64 類型:

>>> df.astype('int64')
   a  b
0  1  1
1  2  2
2  3  3

將 a 列轉換為浮點類型:

>>> df.astype({'a': float})
     a  b
0  1.0  1
1  2.0  2
2  3.0  3

相關用法


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