[Pandas](EN) Use function to each row in DataFrame using apply


Environment and Prerequisite

  • Python
  • Pandas


Usage and Example

  • Use apply in DataFrame
  • Add function to apply as argument
  • Use axis=1 to use per row
  • Parameter in function represents each row in DataFrame
import pandas as pd

# make 20 rows of data
data = {'col1': [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
        'col2': ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T'],
        'col3': [True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False, True, False]}

# make dataframe
df = pd.DataFrame(data)

# define function which will use for apply
# access column by column name
def combine_values_by_column_name(row):
    return str(row['col1']) + row['col2'] + str(row['col3'])

# define function which will use for apply
# access column by index
def combine_values_by_column_name(row):
    return str(row[0]) + row[1] + str(row[2])

# use apply
# watch out for axis value
df['new_col_by_name'] = df.apply(combine_values, axis=1)
df['new_col_by_index'] = df.apply(combine_values, axis=1)

# show result
df.head()
  • Result
  col1 col2 col3 new_col_by_name new_col_by_index
0 1 A True 1ATrue 1ATrue
1 2 B False 2BFalse 2BFalse
2 3 C True 3CTrue 3CTrue
3 4 D False 4DFalse 4DFalse
4 5 E True 5ETrue 5ETrue


Reference