i want to concatenate/join many columns include Nan value to one new column.
how to avoid/pass the NaN in join result?
below just to show my try i used both .agg and .apply.
import pandas as pd
import numpy as np
df = pd.DataFrame({'foo':['a',np.nan,'c'], 'bar':[1, 2, 3], 'new':['apple', 'banana', 'pear']})
subcat_names=["foo","new"]
df["result"] = df[subcat_names].astype(str).agg(','.join, axis=1)
df=df.fillna("")
df["result_2"] =df[subcat_names].apply(lambda x : '{},{}'.format(x[0],x[1]), axis=1)
print(df)
foo bar new result result_2
0 a 1 apple a,apple a,apple
1 2 banana nan,banana ,banana
2 c 3 pear c,pear c,pear
at result the nan, is unwanted
at result_2 , is unwanted
thanks