bpd.DataFrame.assign

DataFrame.assign(**kwargs)[source]

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 (dict of {str: callable or Series}) – The column names are keywords. If the values are callable, they are computed on the DataFrame and assigned to the new columns. The callable must not change input DataFrame (though pandas doesn’t check it). If the values are not callable, (e.g. a Series, scalar, or array), they are simply assigned.
Returns:df_with_cols – A new DataFrame with the new columns in addition to all the existing columns.
Return type:DataFrame
Raises:ValueError – If columns have different lengths or if new columns have different lengths than the existing DataFrame

Examples

>>> df = bpd.DataFrame().assign(flower=['sunflower', 'rose'])
>>> df.assign(color=['yellow', 'red'])
      flower   color
0  sunflower  yellow
1       rose     red