Haraball.no

Apply a function to every row in a Pandas DataFrame

2016-11-17

Create a DataFrame to run functions on:

import pandas as pd

data = {
    'one': [1, 1, 1], 
    'two': [2, 2, 2], 
    'three': [3, 3, 3]
}

df = pd.DataFrame(data)

print(df)

#    one  three  two
# 0    1      3    2
# 1    1      3    2
# 2    1      3    2

Define a function to apply to the column named three in the DataFrame:

def double_threes(row):
    return row['three'] * 2

Run the function on each row using apply. This assigns the result of the function to the three column.

df['three'] = df.apply(double_threes, axis=1)

print(df)

#    one  three  two
# 0    1      6    2
# 1    1      6    2
# 2    1      6    2

Add the results to a new column instead:

df['double_three'] = df.apply(double_threes, axis=1)

print(df)

#    one  three  two  double_three
# 0    1      3    2             6
# 1    1      3    2             6
# 2    1      3    2             6