I want to add new column based on row condition which is based on two different columns of same dataframe.
I have below Dataframe -
df1_data = {'e_id': {0:'101',1:'',2:'103',3:'',4:'105',5:'',6:''},
'r_id': {0:'',1:'502',2:'',3:'504',4:'',5:'506',6:''}}
df=pd.DataFrame(df1_data)
print df
I want to add new column named as "sym".
Condition -
- If 'e_id' column value is not null then sym column value is 'e_id' column value.
- If 'r_id' column value is not null then sym column value is 'r_id' column value.
- If 'e_id' and 'r_id' both column values are null then remove this particular row from pandas dataframe.
I tried with below code -
df1_data = {'e_id': {0:'101',1:'',2:'103',3:'',4:'105',5:''},
'r_id': {0:'',1:'502',2:'',3:'504',4:'',5:'506'}}
df=pd.DataFrame(df1_data)
print df
if df['e_id'].any():
df['sym'] = df['e_id']
print df
if df['r_id'].any():
df['sym'] = df['r_id']
print df
But it is giving me a wrong output.
Expected output -
e_id r_id sym
0 101 101
1 502 502
2 103 103
3 504 504
4 105 105
5 506 506