Skip to content

Instantly share code, notes, and snippets.

@shahifaqeer
Created April 26, 2015 09:22
Show Gist options
  • Save shahifaqeer/44d8ebc0696c93653b17 to your computer and use it in GitHub Desktop.
Save shahifaqeer/44d8ebc0696c93653b17 to your computer and use it in GitHub Desktop.
Apply a function to multiple columns in a pandas dataframe using the axis=1 to do row-wise application!
import pandas as pd
df = pd.DataFrame({'ID':['1','2','3'], 'J1': [0,2,3], 'J2':[1,4,5]})
print df
lst = ['a','b','c','d','e','f']
df['J3'] = df.apply(lambda row:lst[row['J1']:row['J2']],axis=1)
print df
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment