- API reference
- DataFrame
- pandas.DataF...
pandas.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:
- **kwargsdict of {str: callable or Series}
The column names are keywords. If the values arecallable, they are computed on the DataFrame andassigned to the new columns. The callable must notchange 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 toall the existing columns.
Notes
Assigning multiple columns within the same
assign
is possible.Later items in ‘**kwargs’ may refer to newly created or modifiedcolumns in ‘df’; items are computed and assigned into ‘df’ in order.Examples
>>>df=pd.DataFrame({'temp_c':[17.0,25.0]},...index=['Portland','Berkeley'])>>>df temp_cPortland 17.0Berkeley 25.0
Where the value is a callable, evaluated ondf:
>>>df.assign(temp_f=lambdax:x.temp_c*9/5+32) temp_c temp_fPortland 17.0 62.6Berkeley 25.0 77.0
Alternatively, the same behavior can be achieved by directlyreferencing an existing Series or sequence:
>>>df.assign(temp_f=df['temp_c']*9/5+32) temp_c temp_fPortland 17.0 62.6Berkeley 25.0 77.0
You can create multiple columns within the same assign where oneof the columns depends on another one defined within the same assign:
>>>df.assign(temp_f=lambdax:x['temp_c']*9/5+32,...temp_k=lambdax:(x['temp_f']+459.67)*5/9) temp_c temp_f temp_kPortland 17.0 62.6 290.15Berkeley 25.0 77.0 298.15