Set item(s) in DataFrame by key. This method allows you to set the values of one or more columns in the DataFrame using a key. If the key does not exist, a new column will be created. Parameters ---------- key : The object(s) in the index wh
(self, key, value)
| 4557 | self._iset_item_mgr(loc, arraylike, inplace=False, refs=refs) |
| 4558 | |
| 4559 | def __setitem__(self, key, value) -> None: |
| 4560 | """ |
| 4561 | Set item(s) in DataFrame by key. |
| 4562 | |
| 4563 | This method allows you to set the values of one or more columns in the |
| 4564 | DataFrame using a key. If the key does not exist, a new |
| 4565 | column will be created. |
| 4566 | |
| 4567 | Parameters |
| 4568 | ---------- |
| 4569 | key : The object(s) in the index which are to be assigned to |
| 4570 | Column label(s) to set. Can be a single column name, list of column names, |
| 4571 | or tuple for MultiIndex columns. |
| 4572 | value : scalar, array-like, Series, or DataFrame |
| 4573 | Value(s) to set for the specified key(s). |
| 4574 | |
| 4575 | Returns |
| 4576 | ------- |
| 4577 | None |
| 4578 | This method does not return a value. |
| 4579 | |
| 4580 | See Also |
| 4581 | -------- |
| 4582 | DataFrame.loc : Access and set values by label-based indexing. |
| 4583 | DataFrame.iloc : Access and set values by position-based indexing. |
| 4584 | DataFrame.assign : Assign new columns to a DataFrame. |
| 4585 | |
| 4586 | Notes |
| 4587 | ----- |
| 4588 | When assigning a Series to a DataFrame column, pandas aligns the Series |
| 4589 | by index labels, not by position. This means: |
| 4590 | |
| 4591 | * Values from the Series are matched to DataFrame rows by index label |
| 4592 | * If a Series index label doesn't exist in the DataFrame index, it's ignored |
| 4593 | * If a DataFrame index label doesn't exist in the Series index, NaN is assigned |
| 4594 | * The order of values in the Series doesn't matter; only the index labels matter |
| 4595 | |
| 4596 | Examples |
| 4597 | -------- |
| 4598 | Basic column assignment: |
| 4599 | |
| 4600 | >>> df = pd.DataFrame({"A": [1, 2, 3]}) |
| 4601 | >>> df["B"] = [4, 5, 6] # Assigns by position |
| 4602 | >>> df |
| 4603 | A B |
| 4604 | 0 1 4 |
| 4605 | 1 2 5 |
| 4606 | 2 3 6 |
| 4607 | |
| 4608 | Series assignment with index alignment: |
| 4609 | |
| 4610 | >>> df = pd.DataFrame({"A": [1, 2, 3]}, index=[0, 1, 2]) |
| 4611 | >>> s = pd.Series([10, 20], index=[1, 3]) # Note: index 3 doesn't exist in df |
| 4612 | >>> df["B"] = s # Assigns by index label, not position |
| 4613 | >>> df |
| 4614 | A B |
| 4615 | 0 1 NaN |
| 4616 | 1 2 10.0 |
nothing calls this directly
no test coverage detected