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


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

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

用法:

DataFrame.swapaxes(i: Union[int, str], j: Union[int, str], copy: bool = True) → pyspark.pandas.frame.DataFrame

適當地交換軸和交換值軸。

注意

由於大數據的性質,這種方法基於昂貴的操作。在內部,它需要為每個值生成每一行,然後分組兩次——這是一個巨大的操作。為防止誤用,此方法具有“compute.max_rows”默認輸入長度限製,並引發 ValueError。

>>> from pyspark.pandas.config import option_context
>>> with option_context('compute.max_rows', 1000):  
...     ps.DataFrame({'a': range(1001)}).swapaxes(i=0, j=1)
Traceback (most recent call last):
  ...
ValueError: Current DataFrame has more then the given limit 1000 rows.
Please set 'compute.max_rows' by using 'pyspark.pandas.config.set_option'
to retrieve to retrieve more than 1000 rows. Note that, before changing the
'compute.max_rows', this operation is considerably expensive.

參數

i: {0 or ‘index’, 1 or ‘columns’}. The axis to swap.
j: {0 or ‘index’, 1 or ‘columns’}. The axis to swap.
copy布爾值,默認為真。

返回

DataFrame

例子

>>> psdf = ps.DataFrame(
...     [[1, 2, 3], [4, 5, 6], [7, 8, 9]], index=['x', 'y', 'z'], columns=['a', 'b', 'c']
... )
>>> psdf
   a  b  c
x  1  2  3
y  4  5  6
z  7  8  9
>>> psdf.swapaxes(i=1, j=0)
   x  y  z
a  1  4  7
b  2  5  8
c  3  6  9
>>> psdf.swapaxes(i=1, j=1)
   a  b  c
x  1  2  3
y  4  5  6
z  7  8  9

相關用法


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