apply a function to dataframe rows

In [1]:
import pandas as pd
df = pd.DataFrame([['AA', 1],['BB', 2],['CC', 3]], columns = ['name','value'])

In [2]:
df


Out[2]:
name value
0 AA 1
1 BB 2
2 CC 3

In [3]:
def function_1(val_1, val_2):
    return val_1 + str(val_2)

In [4]:
df['col_a'] = df.apply(lambda row: function_1(row['name'], row['value']), axis=1)

In [5]:
df


Out[5]:
name value col_a
0 AA 1 AA1
1 BB 2 BB2
2 CC 3 CC3

In [6]:
def function_2(row):
    return row['value'] * 2

In [7]:
df['col_b'] = df.apply(lambda row: function_2(row), axis=1)

In [8]:
df


Out[8]:
name value col_a col_b
0 AA 1 AA1 2
1 BB 2 BB2 4
2 CC 3 CC3 6