python - Adding Columns if they exist in the Dataframe: pandas -


i have dataframe, want add 3 columns , make one, , want columns exits in data frame. example, want add following columns

list_id=['id1','id2','id3']  df # df data frame 

i trying following summing "if exists" columns, not able to.

id=sum[[col col in list_id if col in df.columns]]  

and explaining id1, id2 columns can

df = pd.dataframe({'column 1':['a', '', 'c', ' '],'column 2':[' ', 'f', ' ', '']})   , new column id   in[34]: a=df['column 1'] + (df['column 2'])  in[35]:     out[35]:      0         1    f     2    c      3        

all suggestions welcome

iiuc 1 possible solution strip wtihespaces:

a=df['column 1'].str.strip() + df['column 2'].str.strip() print (a) 0    1    f 2    c 3      dtype: object 

more general solution first filter column names:

import pandas pd  df = pd.dataframe({'id1':['    a', '', 'c', ' '],                    'id2':[' ', 'f', ' ', ''],                     'id5':['t', 'e', ' ', '']})  print (df)      id1 id2 id5 0            t 1          f   e 2      c         3        list_id=['id1','id2','id3']  cols = df.columns[df.columns.isin(list_id)] print (cols) index(['id1', 'id2'], dtype='object')  #there whitespaces print (df[cols].sum(axis=1)) 0         1         f 2        c  3           dtype: object 

and need apply function strip each column list comprehension, concat output list , last sum columns (axis=1)

print (pd.concat([df[c].str.strip() c in df[cols]], axis=1).sum(axis=1)) 0    1    f 2    c 3      

edit comment:

import pandas pd  df = pd.dataframe({'id1':[15.3, 12.1, 13.2, 10.0],                    'id2':[7.0, 7.7, 2, 11.3],                     'id5':[10, 15, 3.1, 2.2]})   print (df)     id1   id2   id5 0  15.3   7.0  10.0 1  12.1   7.7  15.0 2  13.2   2.0   3.1 3  10.0  11.3   2.2  list_id=['id1','id2','id3'] cols = df.columns[df.columns.isin(list_id)] print (cols) index(['id1', 'id2'], dtype='object') 
#summed floats print (df[cols].sum(axis=1)) 0    22.3 1    19.8 2    15.2 3    21.3 dtype: float64  #cast float string , sum print (df[cols].astype(str).sum(axis=1)) 0     15.37.0 1     12.17.7 2     13.22.0 3    10.011.3 dtype: object  #cast float int, str, sum, removed float 0 cast int , last str print (df[cols].astype(int).astype(str).sum(axis=1).astype(int).astype(str)) 0     157 1     127 2     132 3    1011 dtype: object  #cast float int, str , concanecate join print (df[cols].astype(int).astype(str).apply(lambda x: ''.join(x), axis=1)) 0     157 1     127 2     132 3    1011 dtype: object 

Comments