Examples

In [1]:
import numpy as np
import pandas as pd
In [2]:
df = pd.DataFrame({'temp_c': [7.8, 17.2]},
                  index=['Malaysia', 'Maldives'])
df
Out[2]:
temp_c
Malaysia 7.8
Maldives 17.2

Pandas: DataFrame - assign.

Where the value is a callable, evaluated on df:

In [3]:
df.assign(temp_f=lambda x: x.temp_c * 9 / 5 + 32)
Out[3]:
temp_c temp_f
Malaysia 7.8 46.04
Maldives 17.2 62.96

Pandas: DataFrame - Where the value is a callable, evaluated on df:.

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

In [4]:
df.assign(temp_f=df['temp_c'] * 9 / 5 + 32)
Out[4]:
temp_c temp_f
Malaysia 7.8 46.04
Maldives 17.2 62.96