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()