当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Python pyspark DataFrame.align用法及代码示例


本文简要介绍 pyspark.pandas.DataFrame.align 的用法。

用法:

DataFrame.align(other: Union[DataFrame, Series], join: str = 'outer', axis: Union[int, str, None] = None, copy: bool = True) → Tuple[DataFrame, Union[DataFrame, Series]]

使用指定的连接方法将两个对象在其轴上对齐。

为每个轴索引指定连接方法。

参数

otherDataFrame 或系列
join{{‘outer’, ‘inner’, ‘left’, ‘right’}},默认 ‘outer’
axis其他对象的允许轴,默认无

对齐索引 (0)、列 (1) 或两者(无)。

copy布尔值,默认为真

总是返回新对象。如果 copy=False 并且不需要重新索引,则返回原始对象。

返回

(left, right)(DataFrame,其他类型)

对齐的对象。

例子

>>> ps.set_option("compute.ops_on_diff_frames", True)
>>> df1 = ps.DataFrame({"a": [1, 2, 3], "b": ["a", "b", "c"]}, index=[10, 20, 30])
>>> df2 = ps.DataFrame({"a": [4, 5, 6], "c": ["d", "e", "f"]}, index=[10, 11, 12])

对齐两个轴:

>>> aligned_l, aligned_r = df1.align(df2)
>>> aligned_l.sort_index()
      a     b   c
10  1.0     a NaN
11  NaN  None NaN
12  NaN  None NaN
20  2.0     b NaN
30  3.0     c NaN
>>> aligned_r.sort_index()
      a   b     c
10  4.0 NaN     d
11  5.0 NaN     e
12  6.0 NaN     f
20  NaN NaN  None
30  NaN NaN  None

仅对齐轴 = 0(索引):

>>> aligned_l, aligned_r = df1.align(df2, axis=0)
>>> aligned_l.sort_index()
      a     b
10  1.0     a
11  NaN  None
12  NaN  None
20  2.0     b
30  3.0     c
>>> aligned_r.sort_index()
      a     c
10  4.0     d
11  5.0     e
12  6.0     f
20  NaN  None
30  NaN  None

仅对齐轴 = 1(列):

>>> aligned_l, aligned_r = df1.align(df2, axis=1)
>>> aligned_l.sort_index()
    a  b   c
10  1  a NaN
20  2  b NaN
30  3  c NaN
>>> aligned_r.sort_index()
    a   b  c
10  4 NaN  d
11  5 NaN  e
12  6 NaN  f

与连接类型 “inner” 对齐:

>>> aligned_l, aligned_r = df1.align(df2, join="inner")
>>> aligned_l.sort_index()
    a
10  1
>>> aligned_r.sort_index()
    a
10  4

与系列对齐:

>>> s = ps.Series([7, 8, 9], index=[10, 11, 12])
>>> aligned_l, aligned_r = df1.align(s, axis=0)
>>> aligned_l.sort_index()
      a     b
10  1.0     a
11  NaN  None
12  NaN  None
20  2.0     b
30  3.0     c
>>> aligned_r.sort_index()
10    7.0
11    8.0
12    9.0
20    NaN
30    NaN
dtype: float64
>>> ps.reset_option("compute.ops_on_diff_frames")

相关用法


注:本文由纯净天空筛选整理自spark.apache.org大神的英文原创作品 pyspark.pandas.DataFrame.align。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。