polars.DataFrame.update
-
Update the values in this
DataFramewith the values inother.Warning
This functionality is considered unstable. It may be changed at any point without it being considered a breaking change.
- Parameters:
-
- other
-
DataFrame that will be used to update the values
- on
-
Column names that will be joined on. If set to
None(default), the implicit row index of each frame is used as a join key. -
how{‘left’, ‘inner’, ‘full’} -
- ‘left’ will keep all rows from the left table; rows may be duplicated if multiple rows in the right frame match the left row’s key.
- ‘inner’ keeps only those rows where the key exists in both frames.
- ‘full’ will update existing rows where the key matches while also adding any new rows contained in the given frame.
- left_on
-
Join column(s) of the left DataFrame.
- right_on
-
Join column(s) of the right DataFrame.
- include_nulls
-
Overwrite values in the left frame with null values from the right frame. If set to
False(default), null values in the right frame are ignored. -
maintain_order{‘none’, ‘left’, ‘right’, ‘left_right’, ‘right_left’} -
Which order of rows from the inputs to preserve. See
join()for details. Unlikejointhis function preserves the left order by default.
Notes
This is syntactic sugar for a left/inner join that preserves the order of the left
DataFrameby default, with an optional coalesce wheninclude_nulls = False.Examples
>>> df = pl.DataFrame( ... { ... "A": [1, 2, 3, 4], ... "B": [400, 500, 600, 700], ... } ... ) >>> df shape: (4, 2) ┌─────┬─────┐ │ A ┆ B │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪═════╡ │ 1 ┆ 400 │ │ 2 ┆ 500 │ │ 3 ┆ 600 │ │ 4 ┆ 700 │ └─────┴─────┘ >>> new_df = pl.DataFrame( ... { ... "B": [-66, None, -99], ... "C": [5, 3, 1], ... } ... )Update
dfvalues with the non-null values innew_df, by row index:>>> df.update(new_df) shape: (4, 2) ┌─────┬─────┐ │ A ┆ B │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪═════╡ │ 1 ┆ -66 │ │ 2 ┆ 500 │ │ 3 ┆ -99 │ │ 4 ┆ 700 │ └─────┴─────┘
Update
dfvalues with the non-null values innew_df, by row index, but only keeping those rows that are common to both frames:>>> df.update(new_df, how="inner") shape: (3, 2) ┌─────┬─────┐ │ A ┆ B │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪═════╡ │ 1 ┆ -66 │ │ 2 ┆ 500 │ │ 3 ┆ -99 │ └─────┴─────┘
Update
dfvalues with the non-null values innew_df, using a full outer join strategy that defines explicit join columns in each frame:>>> df.update(new_df, left_on=["A"], right_on=["C"], how="full") shape: (5, 2) ┌─────┬─────┐ │ A ┆ B │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪═════╡ │ 1 ┆ -99 │ │ 2 ┆ 500 │ │ 3 ┆ 600 │ │ 4 ┆ 700 │ │ 5 ┆ -66 │ └─────┴─────┘
Update
dfvalues including null values innew_df, using a full outer join strategy that defines explicit join columns in each frame:>>> df.update(new_df, left_on="A", right_on="C", how="full", include_nulls=True) shape: (5, 2) ┌─────┬──────┐ │ A ┆ B │ │ --- ┆ --- │ │ i64 ┆ i64 │ ╞═════╪══════╡ │ 1 ┆ -99 │ │ 2 ┆ 500 │ │ 3 ┆ null │ │ 4 ┆ 700 │ │ 5 ┆ -66 │ └─────┴──────┘
DataFrame.update(
other: DataFrame,
on: str | Sequence[str] | None = None,
how: Literal['left',
'inner',
'full'] = 'left',
*,
left_on: str | Sequence[str] | None = None,
right_on: str | Sequence[str] | None = None,
include_nulls: bool = False,
maintain_order: MaintainOrderJoin | None = 'left',
) → DataFrame
© 2020 Ritchie Vink
© 2022 Polars contributors
Licensed under the MIT License.
https://docs.pola.rs/api/python/stable/reference/dataframe/api/polars.DataFrame.update.html