- apply
Combine columns to create a new column
examples/pandas/data.csv
fname,lname,age Foo,Bar,100 Alma,Matter,78 Buzz,Lightyear,23
examples/pandas/create_column.py
import pandas as pd filename = 'data.csv' df = pd.read_csv(filename) print(df) print() def combine_two_columns(row): return row['lname'] + '_' + row['fname'] df['combined'] = df.apply(combine_two_columns, axis=1) print(df) print() def combine_more_columns(row): columns = ['lname', 'age', 'fname'] return '_'.join(map(lambda name: str(row[name]), columns)) df['combined'] = df.apply(combine_more_columns, axis=1) print(df)
fname lname age 0 Foo Bar 100 1 Alma Matter 78 2 Buzz Lightyear 23 fname lname age combined 0 Foo Bar 100 Bar_Foo 1 Alma Matter 78 Matter_Alma 2 Buzz Lightyear 23 Lightyear_Buzz fname lname age combined 0 Foo Bar 100 Bar_100_Foo 1 Alma Matter 78 Matter_78_Alma 2 Buzz Lightyear 23 Lightyear_23_Buzz