pandas 1.4.2

NotesParametersReturnsBackRef
assign(self, **kwargs) -> 'DataFrame'

Returns a new object with all original columns in addition to new ones. Existing columns that are re-assigned will be overwritten.

Notes

Assigning multiple columns within the same assign is possible. Later items in '\*\*kwargs' may refer to newly created or modified columns in 'df'; items are computed and assigned into 'df' in order.

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

DataFrame

A new DataFrame with the new columns in addition to all the existing columns.

Assign new columns to a DataFrame.

Examples

This example is valid syntax, but we were not able to check execution
>>> df = pd.DataFrame({'temp_c': [17.0, 25.0]},
...  index=['Portland', 'Berkeley'])
... df temp_c Portland 17.0 Berkeley 25.0

Where the value is a callable, evaluated on :None:None:`df`:

This example is valid syntax, but we were not able to check execution
>>> df.assign(temp_f=lambda x: x.temp_c * 9 / 5 + 32)
          temp_c  temp_f
Portland    17.0    62.6
Berkeley    25.0    77.0

Alternatively, the same behavior can be achieved by directly referencing an existing Series or sequence:

This example is valid syntax, but we were not able to check execution
>>> df.assign(temp_f=df['temp_c'] * 9 / 5 + 32)
          temp_c  temp_f
Portland    17.0    62.6
Berkeley    25.0    77.0

You can create multiple columns within the same assign where one of the columns depends on another one defined within the same assign:

This example is valid syntax, but we were not able to check execution
>>> df.assign(temp_f=lambda x: x['temp_c'] * 9 / 5 + 32,
...  temp_k=lambda x: (x['temp_f'] + 459.67) * 5 / 9) temp_c temp_f temp_k Portland 17.0 62.6 290.15 Berkeley 25.0 77.0 298.15
See :

Back References

The following pages refer to to this document either explicitly or contain code examples using this.

pandas.core.frame.DataFrame.eval

Local connectivity graph

Hover to see nodes names; edges to Self not shown, Caped at 50 nodes.

Using a canvas is more power efficient and can get hundred of nodes ; but does not allow hyperlinks; , arrows or text (beyond on hover)

SVG is more flexible but power hungry; and does not scale well to 50 + nodes.

All aboves nodes referred to, (or are referred from) current nodes; Edges from Self to other have been omitted (or all nodes would be connected to the central node "self" which is not useful). Nodes are colored by the library they belong to, and scaled with the number of references pointing them


File: /pandas/core/frame.py#4447
type: <class 'function'>
Commit: