r""" Assign new columns to a DataFrame. Returns a new object with all original columns in addition to new ones. Existing columns that are re-assigned will be overwritten. Parameters ---------- **kwargs : callable or Series The column name
(self, **kwargs)
| 5649 | self._mgr.insert(loc, column, value, refs=refs) |
| 5650 | |
| 5651 | def assign(self, **kwargs) -> DataFrame: |
| 5652 | r""" |
| 5653 | Assign new columns to a DataFrame. |
| 5654 | |
| 5655 | Returns a new object with all original columns in addition to new ones. |
| 5656 | Existing columns that are re-assigned will be overwritten. |
| 5657 | |
| 5658 | Parameters |
| 5659 | ---------- |
| 5660 | **kwargs : callable or Series |
| 5661 | The column names are keywords. If the values are |
| 5662 | callable, they are computed on the DataFrame and |
| 5663 | assigned to the new columns. The callable must not |
| 5664 | change input DataFrame (though pandas doesn't check it). |
| 5665 | If the values are not callable, (e.g. a Series, scalar, or array), |
| 5666 | they are simply assigned. |
| 5667 | |
| 5668 | Returns |
| 5669 | ------- |
| 5670 | DataFrame |
| 5671 | A new DataFrame with the new columns in addition to |
| 5672 | all the existing columns. |
| 5673 | |
| 5674 | See Also |
| 5675 | -------- |
| 5676 | DataFrame.loc : Select a subset of a DataFrame by labels. |
| 5677 | DataFrame.iloc : Select a subset of a DataFrame by positions. |
| 5678 | |
| 5679 | Notes |
| 5680 | ----- |
| 5681 | Assigning multiple columns within the same ``assign`` is possible. |
| 5682 | Later items in '\*\*kwargs' may refer to newly created or modified |
| 5683 | columns in 'df'; items are computed and assigned into 'df' in order. |
| 5684 | |
| 5685 | Examples |
| 5686 | -------- |
| 5687 | >>> df = pd.DataFrame({"temp_c": [17.0, 25.0]}, index=["Portland", "Berkeley"]) |
| 5688 | >>> df |
| 5689 | temp_c |
| 5690 | Portland 17.0 |
| 5691 | Berkeley 25.0 |
| 5692 | |
| 5693 | Where the value is a callable, evaluated on `df`: |
| 5694 | |
| 5695 | >>> df.assign(temp_f=lambda x: x.temp_c * 9 / 5 + 32) |
| 5696 | temp_c temp_f |
| 5697 | Portland 17.0 62.6 |
| 5698 | Berkeley 25.0 77.0 |
| 5699 | |
| 5700 | Alternatively, the same behavior can be achieved by directly |
| 5701 | referencing an existing Series or sequence: |
| 5702 | |
| 5703 | >>> df.assign(temp_f=df["temp_c"] * 9 / 5 + 32) |
| 5704 | temp_c temp_f |
| 5705 | Portland 17.0 62.6 |
| 5706 | Berkeley 25.0 77.0 |
| 5707 | |
| 5708 | or by using :meth:`pandas.col`: |