Iterations in Python

By on Iterations in Python

Using apply for single column

def something(val):
    new_value = val+'something'
    return new_value

df['new_column] = df['column'].apply(lambda x: something(x))

Using apply for a whole row

def something_row(row):
    new_value = row['col1']+row['col2']
    return new_value

df['new_column] = df.apply(lambda x: something_row(x), axis = 1)

Fixed iteration

for i in range(5):
    print(i)