I have three Pandas
columns where element are list
. For combining these lists, I can do by explicitly write the name of column and +
them together
df = pd.DataFrame({'allmz':([[1,2,3],[2,4,5],[2,5,5],[2,3,5],[1,4,5]]),'allint':([[11,31,31],[21,41,51],[41,51,51],[11,31,51],[1,51,11]]), 'allx':([[6,7,3],[2,4,5],[2,5,5],[2,9,5],[3,4,5]])})
df['new'] = df['allmz'] + df['allint'] + df['allint']
print df
allint allmz allx new
0 [11, 31, 31] [1, 2, 3] [6, 7, 3] [1, 2, 3, 11, 31, 31, 11, 31, 31]
1 [21, 41, 51] [2, 4, 5] [2, 4, 5] [2, 4, 5, 21, 41, 51, 21, 41, 51]
2 [41, 51, 51] [2, 5, 5] [2, 5, 5] [2, 5, 5, 41, 51, 51, 41, 51, 51]
3 [11, 31, 51] [2, 3, 5] [2, 9, 5] [2, 3, 5, 11, 31, 51, 11, 31, 51]
4 [1, 51, 11] [1, 4, 5] [3, 4, 5] [1, 4, 5, 1, 51, 11, 1, 51, 11]
However, if I have too many column names to write each of them, is there a way to do it by looping (or not looping) the list of column name:
columns = ['allmz','allint','allx']
instead?